Share on Google+Share on Google+

Zend FrameWork Part-7

In this tutorial we will study about Zend Framwork of PHP, model section of ZF. Coding section will make it more clear, subsequent pages will discuss on other and advance topic.

Zend Framework-7


In ZF there is no such Model class is available because the model section actually holds the business logic and we should develop that by us. In ZF there are so many previously developed components are available and all we need to do is accessing the right component at right time.

In the current tutorial we will create a model class which will extend Zend_Db_Table and we will use Zend_Db_Table_Row. Zend_Db_Table implements the Table Data Gateway Design Pattern. We will extend the Zend_Db_Table_Abstract class which is an abstract class. There is no such hard-and-fast rule to assign any name to the class but we should call it after the database name. Since this file will be stored in applications/models/DbTable/Books.php, so we will call it Applications_Model_Dbtable_Books.

The name of the table in the  Zend_Db_Table should be in the form of protected property as $_name. Most of the table has a primary key called id and Zend_Db_Table considers this property and this reference of the field can be changed if required.

In the following example we will use getBook, updateBook, deleteBook, updateBook methods and  as the name implies that getBook method will be use to retrieve one row from the table, updateBook will update the table and so on.

applications/models/DbTable/Books.php should have the following coding:


class Application_Model_DbTable_Books extends Zend_Db_Table_Abstract


protected $_name='book_sel';

public function getBook($id)




if(! $row){

throw new Exception("Could not find row $id");


return $row->toArray();


public function addBook($author, $publisher, $title){




public function updateBook($id,$author,$publisher,$title){




public function deleteBook($id)







Posted on: April 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.