_select = $select; } /** * Sets the total row count, either directly or through a supplied * query. Without setting this, {@link getPages()} selects the count * as a subquery (SELECT COUNT ... FROM (SELECT ...)). While this * yields an accurate count even with queries containing clauses like * LIMIT, it can be slow in some circumstances. For example, in MySQL, * subqueries are generally slow when using the InnoDB storage engine. * Users are therefore encouraged to profile their queries to find * the solution that best meets their needs. * * @param Zend_Db_Select|integer $totalRowCount Total row count integer * or query * @return Zend_Paginator_Adapter_DbSelect $this * @throws Zend_Paginator_Exception */ public function setRowCount($rowCount) { if ($rowCount instanceof Zend_Db_Select) { $result = $rowCount->query(Zend_Db::FETCH_ASSOC)->fetch(); if (!isset($result[self::ROW_COUNT_COLUMN])) { /** * @see Zend_Paginator_Exception */ require_once 'Zend/Paginator/Exception.php'; throw new Zend_Paginator_Exception('Row count column not found'); } $this->_rowCount = $result[self::ROW_COUNT_COLUMN]; } else if (is_integer($rowCount)) { $this->_rowCount = $rowCount; } else { /** * @see Zend_Paginator_Exception */ require_once 'Zend/Paginator/Exception.php'; throw new Zend_Paginator_Exception('Invalid row count'); } return $this; } /** * Returns an array of items for a page. * * @param integer $offset Page offset * @param integer $itemCountPerPage Number of items per page * @return array */ public function getItems($offset, $itemCountPerPage) { $this->_select->limit($itemCountPerPage, $offset); if ($this->_select instanceof Zend_Db_Table_Select) { return $this->_select->getTable()->fetchAll($this->_select); } else { return $this->_select->query()->fetchAll(); } } /** * Returns the total number of rows in the result set. * * @return integer */ public function count() { if ($this->_rowCount === null) { $expression = new Zend_Db_Expr('COUNT(*) AS ' . self::ROW_COUNT_COLUMN); $rowCount = clone $this->_select; $rowCount->reset(Zend_Db_Select::COLUMNS) ->reset(Zend_Db_Select::ORDER) ->reset(Zend_Db_Select::LIMIT_OFFSET); $rowCount->__toString(); // Workaround for ZF-3719 and related $rowCount->columns($expression); $this->setRowCount($rowCount); } return $this->_rowCount; } }