Feeds

Put some MVC in your PHP

Trial by separation

5 things you didn’t know about cloud backup

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 insertAction, selectAction, updateAction, and 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 insertView.php, selectView.php, updateView.php, and 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 DatabaseController insertAction function create a Zend_Db adapter, which represents a connection with Oracle database, using the Zend_Db factory.

$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 insertView.php using $_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

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() method.

$select = $db->select();

The 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 where() method.

$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); 

Create a 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'); 

Gartner critical capabilities for enterprise endpoint backup

More from The Register

next story
Why has the web gone to hell? Market chaos and HUMAN NATURE
Tim Berners-Lee isn't happy, but we should be
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
'Stop dissing Google or quit': OK, I quit, says Code Club co-founder
And now a message from our sponsors: 'STFU or else'
Microsoft boots 1,500 dodgy apps from the Windows Store
DEVELOPERS! DEVELOPERS! DEVELOPERS! Naughty, misleading developers!
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Best practices for enterprise data
Discussing how technology providers have innovated in order to solve new challenges, creating a new framework for enterprise data.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Advanced data protection for your virtualized environments
Find a natural fit for optimizing protection for the often resource-constrained data protection process found in virtual environments.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?