lithium\data\model\Query::fields()

public method

Set, get or reset fields option for query.

Usage:

// to add a field
$query->fields('created');

// to add several fields
$query->fields(['title','body','modified']);

// to reset fields to none
$query->fields(false);
// should be followed by a 2nd call to fields with required fields

Parameters

  • mixed $fields

    string, array or false

  • boolean $overwrite

    If true, existing fields will be removed before adding $fields.

Returns

array

Returns an array containing all fields added to the query.

Source

	public function fields($fields = null, $overwrite = false) {
		if ($fields === false || $overwrite) {
			$this->_fields = [0 => [], 1 => []];
		}
		if ($fields === null) {
			return array_merge(array_keys($this->_fields[1]), $this->_fields[0]);
		}
		if (!$fields) {
			return $this;
		}
		foreach ((array) $fields as $key => $field) {
			if (is_string($field)) {
				$this->_fields[1][$field] = true;
			} elseif (is_array($field) && !is_numeric($key)) {
				foreach ($field as &$value) {
					$value = "{$key}.{$value}";
				}
				$this->fields($field);
			} else {
				$this->_fields[0][] = $field;
			}
		}
		return $this;
	}