lithium\data\source\database\adapter\PostgreSql::_distinctExport()

protected method

Helper method for PostgreSql::_queryExport() to export data for use in distinct query.

Parameters

  • object $query

    The query object.

Returns

array

Returns an array with the fields as the first value and the orders as the second value.

Source

	protected function _distinctExport($query) {
		$model = $query->model();
		$orders = $query->order();
		$result = [
			'fields' => [],
			'orders' => [],
		];

		if (is_string($orders)) {
			$direction = 'ASC';
			if (preg_match('/^(.*?)\s+((?:A|DE)SC)$/i', $orders, $match)) {
				$orders = $match[1];
				$direction = $match[2];
			}
			$orders = [$orders => $direction];
		}

		if (!is_array($orders)) {
			return array_values($result);
		}

		foreach ($orders as $column => $dir) {
			if (is_int($column)) {
				$column = $dir;
				$dir = 'ASC';
			}

			if ($model && $model::schema($column)) {
				$name = $this->name($query->alias()) . '.' . $this->name($column);
				$alias = $this->name('_' . $query->alias() . '_' . $column . '_');
			} else {
				list($alias, $field) = $this->_splitFieldname($column);
				$name = $this->name($column);
				$alias = $this->name('_' . $alias . '_' . $field . '_');
			}

			$result['fields'][] = "{$name} AS {$alias}";
			$result['orders'][] = "{$alias} {$dir}";
		}
		return array_values($result);
	}