Models are tasked with providing meaning to otherwise raw and unprocessed data (e.g.
user profile).
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 Singular, CamelCase and be
placed in the app/models directory. i.e. a posts model would be app/model/Post.php.
Examples:
{{{
Return all 'post' records
Post::find('all');
Post::all();
With conditions and a limit
Post::find('all', array('conditions' => array('published' => true), 'limit' => 10));
Post::all(array('conditions' => array('published' => true), 'limit' => 10));
Integer count of all 'post' records
Post::find('count');
Post::count(); // This is equivalent to the above.
With conditions
Post::find('count', array('conditions' => array('published' => true)));
Post::count(array('published' => true));
}}}
The actual objects returned from find() calls will depend on the type of datasource in use.
MongoDB, for example, will return results as a Document, while MySQL will return results
as a RecordSet. Both of these classes extend a common data\Collection class, and provide
the necessary abstraction to make working with either type completely transparent.
For data mutation (saving/updating/deleting), the Model class acts as a broker to the proper
objects. When creating a new record, for example, a call to Post::create() will return a
data\model\Record object, which can then be acted upon.
Example:
{{{
$post = Post::create();
$post->author = 'Robert';
$post->title = 'Newest Post!';
$post->content = 'Lithium rocks. That is all.';
$post->save();
}}}