Create a "controllers" directory and a "views" directory in the Apache web server document root
htdocs by default. We shall create the view scripts in the "views" directory and controllers in the "controllers" directory. Create action controller, "database", for the business logic of the MVC application. Create a
DatabaseController class that extends the
Zend_Controller_Action class and add action functions
deleteAction to the class.
The controller actions will be invoked from view scripts, which provide a user interface to specify the table row to be added, selected, updated and deleted. Create the view scripts
deleteView.php in the "views" directory. The view scripts and other resource files are available in a zipped resources file here.
Add a row
First, we shall add a row to the
Catalog table. In the
insertView.php add a form with input fields for the table row to be added. The "action" attribute of the
<form> element specifies "database/insert", which corresponds to the "insert" action of the "database" controller. In the
insertAction function create a
Zend_Db adapter, which represents a connection with Oracle database, using the
$params = array ('host'=>'localhost','username'=>'OE','password'=>'pw','dbname'=>'orcl'); $db=Zend_Db::factory('Oracle', $params);
The first argument specifies the base name for the adapter class - "Oracle" for the Oracle database. The second argument specifies the adapter parameters. Retrieve the input fields specified in the
$_POST['field'] and create an associative array,
$row, for the columns that constitute a row in the database table. Specify the database table to be updated and insert the new row using the
insert() method of the
Zend_Db adapter class.
$table = 'Catalog'; $rowsAffected = $db->insert($table, $row);
The first argument of the
insert() method is the database table and the second argument is the associative array that maps column names to values. Invoke the
insertView.php with URL
http://localhost/views/insertView.php. To add a row specify the column values and click on create.
Adding a row
Retrieve a row
Next, retrieve a row from the catalog table using the Zend Framework. Create a
Zend_Db_Select object from the
Zend_Db adapter object using the
$select = $db->select();
Zend_Db_Select object is used to construct a SQL
SELECT statement. Specify the
FROM clause using the
from() method and the
WHERE clause using the
$select->from('Catalog', '*'); $select->where('ID = ?', $_POST['id']);
Create the SQL query string from the
Zend_Db_Select object using the
_toString() method. Run the SQL query using the
fetchAll() method and query results will be returned as a row set.
$sql = $select->__toString(); $rowset = $db->fetchAll($sql);
Zend_View object to render a view script and specify the directory containing the view scripts. The
Zend_View class represents the "view" component of the model-view-controller pattern.
$view = new Zend_View(); $view->setScriptPath('views');
PHP Frameworks Features Comparison
More than 40 PHP frameworks are available. For a feature comparison please refer
Akelos and Prado support the most features.
To add quoting in a SELECT query PHP Zend framework provides the Zend_Db_Select class, which automatically adds the quoting. PHP Zend framework also provides the quote(), quoteInto(), and quoteIdentifier() functions to add quotes.
To add quoting replace:
$where = "ID ="."'".$_POST['id']."'";
$where = "ID ="."'".$id."'";
Why quote from and link to articles based upon Zend press releases?
I like Zend as a company, I like the framework and I use Zend Optimizer 3.3 and Zend Guard to protect my released code, but that doesn't mean that Zend is the only option.
If we want to spread the gospel of MVC it's more helpful to show the options available and the range of resources that are out there. That way we are more likely to attract greater uptake of the MVC standard.
For example the fact that Zend doesn't have built in user authentication, ajax and active record support may turn some people off. Cake does and it supports PHP4 which could help some developers make the switch when all their legacy code is built upon older versions of PHP. The migration to PHP5 is not to be taken lightly if you have a large and complex web app already deployed on PHP4.
So I say show us all the options and everyone benefits in the long run including Zend.