lithium\data\source\Database::calculation PHP Method

calculation() public method

Executes calculation-related queries, such as those required for count and other aggregates.
public calculation ( string $type, mixed $query, array $options = [] ) : integer | null
$type string Only accepts `count`.
$query mixed The query to be executed.
$options array Optional arguments for the `read()` query that will be executed to obtain the calculation result.
return integer | null Result of the calculation or `null` if the calculation failed.
    public function calculation($type, $query, array $options = array())
    {
        $query->calculate($type);
        switch ($type) {
            case 'count':
                if (strpos($fields = $this->fields($query->fields(), $query), ',') !== false) {
                    $fields = "*";
                }
                $query->fields("COUNT({$fields}) as count", true);
                $query->map(array($query->alias() => array('count')));
                $result = $this->read($query, $options)->data();
                if (!$result || !isset($result[0]['count'])) {
                    return null;
                }
                return (int) $result[0]['count'];
        }
    }