Zend_Db_Table update in ZF1.9

I usually have a bunch of empty Db_Table classes in my Zend Framework apps which just exist to define table names and do the inherited CRUD stuff.

However, with the just released version 1.9 of the framework (currently in beta), I don’t need to do this anymore. Zend_Db_Table is now a concrete implementation that accepts a table name as a parameter and can do all the CRUD stuff without needing a class to be defined.

Obviously, if custom code needs to be added to the Db_Table instance a class needs to be created but this will still go a long way towards cleaning up my models folder.

As an example:

  • The old way
    class Books extends Zend_Db_Table_Abstract {
    	protected $_name = 'books';
    }
    
    // and in the controller (or service layer class in my case)
    $booksTable = new Books();
    $books = $booksTable ->fetchAll();
    
  • The new way
    // no Db_Table subclass needed
    $booksTable = new Zend_Db_Table('books');
    $books = $booksTable ->fetchAll();
    

Another addition to Zend_Db is Zend_Db_Table_Definition and the concrete Zend_Db_Table constructor accepts an instance of this as a second parameter.
All setup code that can be added to an instance of Zend_Db_Table_Abstract (e.g. relationships) can be added through a definition class but the value of this is lost on me as this could lead to the same piece of code being defined in multiple places. In my opinion if the instance requires more than a table name, create a class for it.

Related posts:

  1. Zend_Db_Table dynamic finders
This entry was posted in Zend Framework. 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>