Models expose a consistent and unified API to interact with an underlying datasource (e.g.
MongoDB, CouchDB, MySQL) for operations such as querying, saving, updating and deleting data
from the persistent storage.
Models allow you to interact with your data in two fundamentally different ways: querying, and
data mutation (saving/updating/deleting). All query-related operations may be done through the
static `find()` method, along with some additional utility methods provided for convenience.
Classes extending this one should, conventionally, be named as Plural, CamelCase and be
placed in the `models` directory. i.e. a posts model would be `model/Posts.php`.
// Return all 'post' records
// With conditions and a limit
Posts::find('all', array('conditions' => array('published' => true), 'limit' => 10));
Posts::all(array('conditions' => array('published' => true), 'limit' => 10));
// Integer count of all 'post' records
Posts::count(); // This is equivalent to the above.
// With conditions
Posts::find('count', array('conditions' => array('published' => true)));
Posts::count(array('published' => true));
The actual objects returned from `find()` calls will depend on the type of data source in use.
MongoDB, for example, will return results as a `Document` (as will CouchDB), while MySQL will
return results as a `RecordSet`. Both of these classes extend a common `lithium\data\Collection`
class, and provide the necessary abstraction to make working with either type completely
For data mutation (saving/updating/deleting), the `Model` class acts as a broker to the proper
objects. When creating a new record or document, for example, a call to `Posts::create()` will
return an instance of `lithium\data\entity\Record` or `lithium\data\entity\Document`, which can
then be acted upon.
$post = Posts::create();
$post->author = 'Robert';
$post->title = 'Newest Post!';
$post->content = 'Lithium rocks. That is all.';