Zend FrameWork Part-7


 

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.

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

Model:

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:

<?php

class Application_Model_DbTable_Books extends Zend_Db_Table_Abstract

{

protected $_name='book_sel';

public function getBook($id)

{

$id=(int)$id;

$row=$this->fetchRow('id='.$id);

if(! $row){

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

}

return $row->toArray();

}

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

$data=array('author'=>$author,'publisher'=>$publisher,'title'=>$title);

$this->insert($data);

}

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

$data=array('author'=>$author,'publisher'=>$publisher,'title'=>$title);

$this->update($data,'id='.(int)$id);

}

public function deleteBook($id)

0

{

$this->delete("id=".(int)$id);

}

1

}

 

Ads