Extends
lithium\core\StaticObject
The `find` method allows you to retrieve data from the connected data source.
Examples:
{{{
Model::find('all'); // returns all records
Model::find('count'); // returns a count of all records
// The first ten records that have 'author' set to 'Lithium'
Model::find('all', array(
'conditions' => array('author' => "Lithium"), 'limit' => 10
));
}}}
Parameters
- string $type The find type, which is looked up in `Model::$_finders`. By default it accepts `all`, `first`, `list` and `count`,
- array $options Options for the query. By default, accepts: - `conditions`: The conditional query elements, e.g. `'conditions' => array('published' => true)` - `fields`: The fields that should be retrieved. When set to `null`, defaults to all fields. - `order`: The order in which the data will be returned, e.g. `'order' => 'ASC'`. - `limit`: The maximum number of records to return. - `page`: For pagination of data.
Returns
mixedSource
public static function find($type, array $options = array()) {
$self = static::_object();
$finder = array();
if ($type === null) {
return null;
}
if ($type != 'all' && is_scalar($type) && !isset($self->_finders[$type])) {
$options['conditions'] = array($self->_meta['key'] => $type);
$type = 'first';
}
if (isset($self->_finders[$type]) && is_array($self->_finders[$type])) {
$options = Set::merge($self->_finders[$type], $options);
}
$options = (array) $options + (array) $self->_query;
$meta = array('meta' => $self->_meta, 'name' => get_called_class());
$params = compact('type', 'options');
$filter = function($self, $params) use ($meta) {
$options = $params['options'] + array('type' => 'read', 'model' => $meta['name']);
$query = $self::invokeMethod('_instance', array('query', $options));
return $self::connection()->read($query, $options);
};
if (is_string($type) && isset($self->_finders[$type])) {
$finder = is_callable($self->_finders[$type]) ? array($self->_finders[$type]) : array();
}
return static::_filter(__FUNCTION__, $params, $filter, $finder);
}