让 Zend_Db 支持 MySQL 的 SQL_CALC_FOUND_ROWS
Tuesday, 10. November 2009, 10:15:07
const FOUND_ROWS = 'foundrows';
const SQL_FOUND_ROWS = 'SQL_CALC_FOUND_ROWS';
protected static $_partsInit = array(
self::FOUND_ROWS => false,
... ...
);
/**
* Makes the query SELECT SQL_CALC_FOUND_ROWS.
*
* @param bool $flag Whether or not add SQL_CALC_FOUND_ROWS to SELECT.
* @return Zend_Db_Select This Zend_Db_Select object.
*/
public function foundRows($flag = true)
{
$this->_parts[self::FOUND_ROWS] = (bool) $flag;
return $this;
}
/**
* Render FOUND_ROWS clause
*
* @param string $sql SQL query
* @return string
*/
protected function _renderFoundRows($sql)
{
if ($this->_parts[self::FOUND_ROWS]) {
$sql .= ' ' . self::SQL_FOUND_ROWS;
}
return $sql;
}
以上代码的具体位置不再详述。
调用方法如下:
$select = $db->select()->foundRows()->from(
... ...
)->where(
... ...
);
$data = $db->fetchAll($select);
$stmt = $db->query("SELECT FOUND_ROWS() as cnt");
list($rec) = $stmt->fetchAll();
$count = $rec['cnt'];







