lithium\data\source\Database::calculation()

public method

Executes calculation-related queries, such as those required for count and other aggregates.

When building count queries and a single field is given, will use that to build the COUNT() fragment. When multiple fields are given forces a COUNT(*).

Parameters

  • string $type

    Only accepts count.

  • mixed $query

    The query to be executed.

  • array $options

    Optional arguments for the read() query that will be executed to obtain the calculation result.

Returns

integer|null

Result of the calculation or null if the calculation failed.

Source

	public function calculation($type, $query, array $options = []) {
		$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([$query->alias() => ['count']]);

				$result = $this->read($query, $options)->data();

				if (!$result || !isset($result[0]['count'])) {
					return null;
				}
				return (integer) $result[0]['count'];
		}
	}