mongo_model is a library that adds MongoDB support to RoxPHP. To start using it just download the latest release and drop it to app/libraries/mongo_model
.
As simple as:
\mongo_model\ConnectionManager::setConfig('default', array(
'database' => 'blog',
'server' => 'mongodb://127.0.0.1:27017'
));
To create a MongoDB backed model extend \mongo_model\Model
and define the schema by overriding the _schema
static property.
class Post extends \mongo_model\Model {
protected static $_schema = array(
'title' => 'string',
'body' => 'string',
'author' => 'string'
);
protected function _validate() {
// validation code
}
}
Since \mongo_model\Model
is a \rox\ActiveModel
the validation can be handled via validation callbacks. The interface of \mongo_model\Model
is very similar to \rox\Activerecord
, here are couple examples:
Creating new record:
$post = new Post(array(
'title' => 'Hello',
'body' => 'Hello World!',
'author' => 'James'
));
$post->save();
Finding all records:
$posts = Post::findAll();
Finding records with conditionals:
$posts = Post::findAll(array('conditions' => array('author' => 'James')));
// or
$posts = Post::findAllByAuthor('James');
Finding a single records:
$post = Post::findFirst(array('title' => 'Hello'));
// or
$post = Post::findByTitle('Hello');
Copyright (C) 2011 Ramon Torres. Licensed under The MIT License