/** * Get count * @return integer */ public function count() { $sql = 'SELECT'; if ($this->distinct) { $sql .= ' DISTINCT'; } $sql .= ' COUNT(*) as CNT'; if ($this->entity->getDbTable() === null) { $error = 'Table name is not set class: `' . get_class($this->entity) . '`'; throw new \RuntimeException($error); } $sql .= ' FROM ' . $this->entity->getDbTable() . ' T'; if (!empty($this->relation)) { $relation_array = $this->getRelationArray($this->relation); if (!empty($relation_array)) { $sql .= ' ' . implode(' ', $relation_array); } } if (!empty($this->filter_sql)) { $sql .= ' WHERE ' . implode(' AND ', $this->filter_sql); } if (!empty($this->group_sql)) { $sql .= ' GROUP BY ' . implode(',', $this->group_sql); } $result = $this->query($sql); return intval($result->fetch()['CNT']); }
/** * Prepare array from db * * @param array $fields * @return array */ protected function prepareArrayFromDb(array $fields) { $columns = $this->table->getColumns(); foreach ($fields as $field => &$value) { if ($columns->has($field)) { $value = $columns->get($field)->convertFromDB($value); } } unset($value); return $fields; }