lithium\data\Model::find()
The find
method allows you to retrieve data from the connected data source.
Examples:
Posts::find('all'); // returns all records
Posts::find('count'); // returns a count of all records
// The first ten records that have `'author'` set to `'Bob'`.
Posts::find('all', array(
'conditions' => array('author' => 'Bob'),
'limit' => 10
));
// First record where the id matches 23.
Posts::find('first', array(
'conditions' => array('id' => 23)
));
Shorthands:
// Shorthand for find first by primary key.
Posts::find(23);
// Also works with objects.
Posts::find(new MongoId(23));
Parameters
-
string|object|integer
$type
The name of the finder to use. By default the following finders are available. Custom finders can be added via
Model::finder()
.'all'
: Returns all records matching the conditions.'first'
: Returns the first record matching the conditions.'count'
: Returns an integer count of all records matching the conditions.'list'
: Returns a one dimensional array, where the key is the (primary) key and the value the title of the record (the record must have a'title'
field). A result may look like:array(1 => 'Foo', 2 => 'Bar')
.
Instead of the name of a finder, also supports shorthand usage with an object or integer as the first parameter. When passed such a value it is equal to
Model::find('first', array('conditions' => array('<key>' => <value>)))
.Note: When an undefined finder is tried to be used, the method will not error out, but fallback to the
'all'
finder. -
array
$options
Options for the query. By default, accepts:
'conditions'
array: The conditions for the query i.e.'array('is_published' => true)
.'fields'
array|null: The fields that should be retrieved. When set tonull
and by default, uses all fields. To optimize query performance, limit the fields to just the ones actually needed.'order'
array|string: The order in which the data will be returned, i.e.'created ASC'
sorts by created date in ascending order. To sort by multiple fields use the array syntaxarray('title' => 'ASC', 'id' => 'ASC)
.'limit'
integer: The maximum number of records to return.'page'
integer: Allows to paginate data sets. Specifies the page of the set together with the limit option specifying the number of records per page. The first page starts at1
.
Returns
mixedThe result/s of the find. Actual result depends on the finder being used. Most
often this is an instance of lithium\data\Collection
or lithium\data\Entity
.
Filter
Allows to execute logic before querying (i.e. for rewriting of $options) or after i.e. for caching results.
Source
public static function find($type, array $options = array()) {
$self = static::_object();
if (is_object($type) || !isset($self->_finders[$type])) {
$options['conditions'] = static::key($type);
$type = 'first';
}
$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 (isset($self->_finders[$type])) {
$finder = array($self->_finders[$type]);
} else {
$finder = array();
}
return static::_filter(__FUNCTION__, $params, $filter, $finder);
}