First, a quick tip for working with Zend_Db_Select (and for the extension Zend_Db_Table_Select), you can view the generated SQL by calling the toString method. It is very important to make sure that your code generates the correct query before working with the result set:
$select = $table->select(); $select->from ("table", array("date", "column1" => "sum(column1)")); $select->group ( array ("date") ); $sql = (string) $select;
Or simply
die($select);
I wrote the following test script using your example and have no problems:
class Table extends Zend_Db_Table_Abstract { protected $_primary = 'id'; protected $_name = 'table'; } $db = Zend_Db::factory('Pdo_Mysql', array( 'dbname' => 'test', 'username' => 'root', 'password' => '', 'host' => 'localhost' )); $table = new Table($db); $select = $table->select(); $select->from ($table, array("date", "column1" => new Zend_Db_Expr("sum(column1)"))); $select->group ( array ("date") ); $sql = (string) $select; echo $sql; $results = $table->fetchAll ($select); $result = $results[0]; $date = $result->date; $column1 = $result->column1; echo '<br>' . $date . ': ' . $column1;
Use Zend_Debug :: dump ($ result); to check data inside Zend_Db_Table_Row, if necessary.
In my case, the generated SQL is as follows:
SELECT `table`.`date`, sum(column1) AS `column1` FROM `table` GROUP BY `date`
David Snabel-Caunt
source share