lithium\data\Model::find PHP Method

find() public static method

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));
See also: lithium\data\Model::finder()
public static find ( string | object | integer $type, array $options = [] ) : mixed
$type string | object | integer 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('' => )))`. Note: When an undefined finder is tried to be used, the method will not error out, but fallback to the `'all'` finder.
$options array 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 to `null` 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 syntax `array('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 at `1`.
return mixed The 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`.
    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);
    }

Usage Example

Example #1
0
 /**
  * allows for data-retrieval of entities via file-based access
  *
  * In case you want to provide default file-data without inserting them into the database, you
  * would need to create files based on model-name in a path like that
  * `{:library}/data/{:class}/{:id}.neon` or `{:library}/data/{:class}/{:slug}.neon`
  *
  * In that case, an entity requested by id or slug would be loaded from file instead. Please pay
  * attention, though that not all options are implemented, such as extended conditions, order,
  * limit or page. This is meant to enable basic loading of id- or slug-based entity lookups.
  *
  * @see radium\util\Neon::file()
  * @see radium\util\File::contents()
  * @param string $type The find type, which is looked up in `Model::$_finders`. By default it
  *        accepts `all`, `first`, `list` and `count`,
  * @param 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.
  * @return mixed
  */
 public static function find($type, array $options = array())
 {
     $result = parent::find($type, $options);
     $neon = static::meta('neon');
     if ($neon && (!$result || !count($result))) {
         return Neon::find(get_called_class(), $type, $options);
     }
     return $result;
 }