public function commit()
    {
        try {
            if (empty($this->_fields)) {
                $field = 'COUNT(1)';
            } else {
                $field = 'COUNT(';
                if ($this->_fields[static::FIELD_DISTINCT]) {
                    $field .= 'DISTINCT(';
                }
                $field .= '`' . $this->_fields[static::FIELD_NAME] . '`';
                if ($this->_fields[static::FIELD_DISTINCT]) {
                    $field .= ')';
                }
                $field = ')';
            }

            $query = "
                SELECT
                    $field AS nb
                FROM
                    `" . $this->getDbPrefix() . $this->_dbContainer . "`";

            if ($this->_conditions->count()) {
                $query .= "
                WHERE
                    " . $this->_conditions->getPreparedConditions($this->_dbContainer) . "";
            }

            $prepared = Agl::app()->getDb()->getConnection()->prepare($query);

            if ($prepared->execute($this->_conditions->getPreparedValues())) {
                $result = $prepared->fetchObject();
                if (! $result) {
                    throw new Exception("The count query failed (table '" . $this->getDbPrefix() . $this->_dbContainer . "')");
                }
            } else {
                $error = $prepared->errorInfo();
                throw new Exception("The count query failed (table '" . $this->getDbPrefix() . $this->_dbContainer . "') with message '" . $error[2] . "'");
            }

            if (Agl::app()->isDebugMode()) {
                Agl::app()->getDb()->incrementCounter();
            }

            return (int)$result->{'nb'};
        } catch (Exception $e) {
            throw new Exception($e);
        }
    }