/** * Creates and populates the DataSource * * @access public * @return void **/ public function setup() { $select = $this->getSelect(); if ($this->paginate) { $sql = $this->getSelectCountSql(); $this->totalRows = (int) $this->_db->fetchOne($sql); list($start, $total) = $this->limit; $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $select->limit($total, $start); } else { $this->totalRows = 0; } $select->reset(Zend_Db_Select::ORDER); if (count($this->order) > 0) { $select->order($this->order); } // Fetch Select Columns $rawColumns = $select->getPart(Zend_Db_Select::COLUMNS); $columns = array(); // Get columns and Force casting as strings foreach ($rawColumns as $col) { $columns[] = (string) $col[1]; } $this->cols = $columns; $this->totalColumns = count($columns); // Fetch $stmt = $this->_db->query($select); $rows = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); $total = count($rows); $this->totalRowset = $total; $this->rows = $rows; }
/** * Implements countable, returns count of records */ public function count() { if ($this->_count !== false) { return $this->_count; } // Originally, we chose to replace the selected fields part of the query with a COUNT(*), however // we ran into several conditions where this would not work such as if the query has a join, a limit, // a group by, a sub-select as a field, or orders by a derived field. Therefore, we do a sub-select // of our own to find the count. $this->_count = (int) $this->_zendDb->fetchOne('SELECT COUNT(*) as rowCount FROM (' . $this->query() . ') as t1'); return $this->_count; }