有幾種指定要在Zend_Db_Select中獲取的列的方法。下面的兩個產品的同一SQL
$select = $db->select()
->from('myTable', array())
->columns(array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));
$select = $db->select()
->from('myTable', array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));
在從方法需要一個第一個參數,表格名,和一個第二個參數,列的陣列來獲取。如果你使用表達式,你可以指定一個'key'=> Expr。
將Zend_Db_Select轉換爲SQL字符串以進行調試或與其他函數一起使用是非常容易的。
echo $select; // prints SELECT COUNT(*) AS `TotalRecords` FROM `myTable`
這將使用toString方法,它是自動調用由Zend_Db的獲取方法:
$total = $db->fetchOne($select);
echo $total; //prints the number of rows matching the query
其中$ db是Zend_Db的實例。