Extends
lithium\core\StaticObject
Allows the use of syntactic-sugar like `Model::all()` instead of `Model::find('all')`.
Parameters
- string $method Method name caught by `__callStatic()`.
- array $params Arguments given to the above `$method` call.
Returns
mixed Results of dispatched `Model::find()` call.Links
Source
public static function __callStatic($method, $params) {
$self = static::_object();
$isFinder = isset($self->_finders[$method]);
if ($isFinder && count($params) === 2 && is_array($params[1])) {
$params = array($params[1] + array($method => $params[0]));
}
if ($method == 'all' || $isFinder) {
if ($params && is_scalar($params[0])) {
$params[0] = array('conditions' => array($self->_meta['key'] => $params[0]));
}
return $self::find($method, $params ? $params[0] : array());
}
preg_match('/^findBy(?P<field>\w+)$|^find(?P<type>\w+)By(?P<fields>\w+)$/', $method, $args);
if (!$args) {
$message = "Method `%s` not defined or handled in class `%s`.";
throw new BadMethodCallException(sprintf($message, $method, get_class($self)));
}
$field = Inflector::underscore($args['field'] ? $args['field'] : $args['fields']);
$type = isset($args['type']) ? $args['type'] : 'first';
$type[0] = strtolower($type[0]);
$conditions = array($field => array_shift($params));
$params = (isset($params[0]) && count($params) == 1) ? $params[0] : $params;
return $self::find($type, compact('conditions') + $params);
}