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

protected method

Reformats fields to be alias based.

Parameters

  • array $fields

    Array of fields.

  • object $context

    Generally a data\model\Query instance.

Returns

array

Reformatted fields

Source

	protected function _fields($fields, $context) {
		$alias = $context->alias();
		$models = $context->models($this);
		$list = [];

		foreach ($fields as $key => $field) {
			if (!is_string($field)) {
				if (isset($models[$key])) {
					$field = array_fill_keys($field, true);
					$list[$key] = isset($list[$key]) ? array_merge($list[$key], $field) : $field;
				} else {
					$list[0][] = is_array($field) ? reset($field) : $field;
				}
				continue;
			}
			if (preg_match('/^([a-z0-9_-]+|\*)$/i', $field)) {
				isset($models[$field]) ? $list[$field]['*'] = true : $list[$alias][$field] = true;
			} elseif (preg_match('/^([a-z0-9_-]+)\.(.*)$/i', $field, $matches)) {
				$list[$matches[1]][$matches[2]] = true;
			} else {
				$list[0][] = $field;
			}
		}
		return $list;
	}