lithium\data\Model::key()

public static method

If no values supplied, returns the name of the Model key. If values are supplied, returns the key value.

Parameters

  • mixed $values

    An array of values or object with values. If $values is null, the meta 'key' of the model is returned.

Returns

mixed

Key value.

Source

	public static function key($values = null) {
		$key = static::meta('key');

		if ($values === null) {
			return $key;
		}

		$self = static::object();
		$entity = $self->_classes['entity'];
		if (is_object($values) && is_string($key)) {
			return static::_key($key, $values, $entity);
		} elseif ($values instanceof $entity) {
			$values = $values->to('array');
		}

		if (!is_array($values) && !is_array($key)) {
			return [$key => $values];
		}

		$key = (array) $key;
		$result = [];
		foreach ($key as $value) {
			if (!isset($values[$value])) {
				return null;
			}
			$result[$value] = $values[$value];
		}
		return $result;
	}