ZF-1335: Provide table-specific SELECT queries with select() method


Original message by Michael Depetrillo:


I was wondering if it made sense to add a select($cols) helper method to Zend_Db_Table that would return an instance of Zend_Db_Select.


Model_Lead extends Zend_Db_Table_Abstract { $_name = 'leads_table'; }

$lead = new Model_Lead();
$select = $lead->select(new Zend_Db_Expr('COUNT(*)')->where('status = ?', 'pending');
echo $select; // select COUNT(*) from leads_table

This would be very helpful in centralizing tables names through models.


There was a typo in my original message. The example above should return "select count(*) from leads_table where status = ?".

A workaround that exists right now is the following:

$lead = new Model_Lead();
$select = $lead->getAdapter()->select();
$select->from($lead->info()['name'], 'COUNT(*)');
$select->where('status = ?', 'pending');

This issue is related to ZF-836, which requests a feature of a table method fetchCount().

We aren't going to address this use case in ZF 1.0. We would like to provide a solution later, but I'm going to postpone this issue for now.

With the new Zend_Db_Table enhancements you can now perform

$lead = new Model_Lead();
$select = $lead->select();
$select->from($lead, 'COUNT(*)')->where('status = ?', 'pending');

Does that satisfy the requirements of this request? If so I'll mark it as complete.

Michael, can you please verify that Simon's solution is adequate?