I agree with you that Zend_Db_Table sometimes acts as a hammer in search of a nail. This is not always the most natural tool for writing custom, executable queries with joins. It seems to me that Zend_Db_Table is optimal primarily for a single table without joins. But you can still perform the optimal db query using aspects of the Zend_Db component.
As @singles noted, all this db access should probably be buried inside the model (for an approach like ActiveRecord) or in mapper (for the DataMapper approach) or in an entity manager (e.g. Doctrine2).
For the exmaple of your BlogPost user model, an instance of the custom BlogPost can be added, which will be loaded by the Zend_Db_Adapter instance. All your requests, including your optimized connections, will be in the BlogPost_DataAccess class. SELECT queries in the DataAccess class can be written using soemthing like (pseudocode):
$select = $adapter->select() ->join() ->join() // etc. ->join() ->where() ->limit() ->order();
See what I mean?
Update
What about the member variable $tableMap ? Something like:
public static $tableMap = array( 'posts' => 'table_posts', 'users' => 'table_users', 'categories' => 'table_categories',
Then in your queries you can use something like:
$select = $adapter->select(); $select->from(self::$tableMap['users'], array('name', 'email'));
Maybe add methods called setTableMap($tableMap) , getTableMap() , getTable($k) , setTable($k) , save the original mapping data in the configuration file, and then populate the static member at boot time.
David Weinraub
source share