A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Extending the Models in Zend Framework = Better readability + Easier to maintain

Let’s say I have a Model named: Psdump.php and it is found in /models/Psdump.php

< ?php
 
/**
 * Psdump.php
 * /opt/apache2/htdocs/apps/org_v2/application/models/Psdump.php
 */
 
require_once APPLICATION_PATH.'/models/PsdumpTable.php'; //I will need this
 
class Psdump extends Zend_Db_Table
{
    protected $_table;    public function getTable()
    {
        if(null === $this->_table) {
            $this->_table = new PsdumpTable;
        }
        return $this->_table;
    }
 
	public function fetchAllEmployees()
	{
		return $this->getTable()->fetchActiveEmployees();
	}
 
}

And for the “extension”…

&lt; ?php
 
/**
 * PsdumpTable
 * /opt/apache2/htdocs/apps/org_v2/application/models/PsdumpTable.php
 *
 */
 
class PsdumpTable extends Zend_Db_Table_Abstract
{
    protected $_name = 'psdump';    public function insert(array $data)
    {
        return parent::insert($data);
    }
 
	public function fetchActiveEmployees()
	{
		$sql = "SELECT * FROM ".$this->_name." WHERE Status=".$this->_db->quote('A');
		return $this->_db->fetchAll($sql);
	}
}

Even though I will have more files — the end result is that this Model setup will be easier to maintain and understand.

Hehe, another post that only I can understand :P

This entry was posted in General and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>