/** * Count the number of records in the table. * * @return integer */ public function count_all() { $selects = array(); $group_by = array(); foreach ($this->_db_pending as $key => $method) { if ($method['name'] == 'select') { // Ignore any selected columns for now $selects[] = $method; unset($this->_db_pending[$key]); } elseif ($method['name'] == 'order_by') { // Also, ignore order clause $order_by[$key] = $method; // Fix of the fix here! unset($this->_db_pending[$key]); } elseif ($method['name'] == 'group_by') { // Ignore any group by for now $group_by[] = $method; unset($this->_db_pending[$key]); } } if (!empty($this->_load_with)) { foreach ($this->_load_with as $alias) { // Bind relationship $this->with($alias); } } $this->_build(Database::SELECT); $sql = $this->_db_builder->from(array($this->_table_name, $this->_object_name)); if (!empty($group_by)) { $prefix = $this->_db->table_prefix(); $sql->selectArgs(array(DB::expr('COUNT(DISTINCT ' . $prefix . $this->_object_name . '.id)'), 'records_found')); } else { $sql->selectArgs(array(DB::expr('COUNT("*")'), 'records_found')); } $records = $sql->execute($this->_db)->get('records_found'); // Add back in selected columns $this->_db_pending = array_merge($this->_db_pending, $selects); // Add back in group by conditions $this->_db_pending = array_merge($this->_db_pending, $group_by); if (isset($order_by)) { // Add back in order_by clause $this->_db_pending = array_merge($this->_db_pending, $order_by); } $this->reset(); // Return the total number of records in a table return $records; }