lithium\data\Model::key()
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
isnull
, the meta'key'
of the model is returned.
Returns
mixedKey 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;
}