public function count()
    {
        if (!$this->isReady()) {
            throw new LogicException('Collection is not ready');
        }

        $table_name = $this->connection->escapeTableName($this->getTableName());
        $conditions = $this->getConditions() ? " WHERE {$this->getConditions()}" : '';

        if ($join_expression = $this->getJoinExpression()) {
            return (int) $this->connection->executeFirstCell("SELECT COUNT($table_name.`id`) FROM $table_name $join_expression $conditions");
        } else {
            return $this->connection->executeFirstCell("SELECT COUNT(`id`) AS 'row_count' FROM $table_name $conditions");
        }
    }