by purekid

purekid / mongodm

MongoDB ORM that includes support for references,embed and multilevel inheritance.

198 Stars 44 Forks Last release: over 3 years ago (v1.5.0) MIT License 411 Commits 6 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


SensioLabsInsight Build Status Latest Stable Version Total Downloads License


Mongodm is a MongoDB ORM that includes support for references,embed and even multilevel inheritance.


  • ORM
  • Simple and flexible
  • Support for embed
  • Support for references (lazy loaded)
  • Support for multilevel inheritance
  • Support for local collection operations


  • PHP 5.3 or greater (Tested for 5.5,5.6,7.0,7.1)
  • Mongodb 1.3 or greater
  • PHP Mongo extension


1. Setup in composer.json:

        "require": {
            "purekid/mongodm": "dev-master"

2. Install by composer:

$ php composer.phar update

Setup Database

Database config file (By default it locates at /vendor/purekid/mongodm/config.php)

    return array(
        'default' => array(
            'connection' => array(
                'hostnames' => 'localhost',
                'database'  => 'default',
    //          'username'  => '',
    //          'password'  => '',
        'production' => array(
            'connection' => array(
                'hostnames' => 'localhost',
                'database'  => 'production',
                'options' => array('replicaSet' => 'rs0')


Authentication information is passed in via the options array. If you do not specifiy authSource, then the PHP Mongo Driver will choose the "admin" database.

$config =  array( 'connection' => array(
      'hostnames' => ':',
      'database'  => '',
      'options'  => [ "connectTimeoutMS" => 500 , "username" => "admin", "password" => "", "authSource" => "admin"] )

Setup database in application

1.You can set up configuration using the

\Purekid\Mongodm\MongoDB::setConfigBlock('default', array(
    'connection' => array(
        'hostnames' => 'localhost',
        'database'  => 'default',
        'options'  => array()

// \Purekid\Mongodm\MongoDB::setConfigBlock('auth', array( 'connection' => array( 'hostnames' => 'localhost', 'database' => 'authDB', 'options' => array() ) ));

2.Or you can duplicate a config file into your project, then define a global constanct 'MONGODM_CONFIG' with it's location.

//in a global initialization place


Choose config section with APPLICATION_ENV

Which config section Mongodm use ? Mongodm choose 'default' section by default.

You have two ways to specify section :

1.'$config' attribute in Model , you can find this attribute in example below.

2.With environment constanct 'APPLICATION_ENV' ,this constanct can be set by webserver,your code or shell environment. In this case,you should set $config='default' or don't declare $config in your own model class.

Create a model and enjoy it

    use Purekid\Mongodm\Model;

class User extends Model 

    static $collection = "user";

    /** use specific config section **/
    public static $config = 'testing';

    /** specific definition for attributes, not necessary! **/
    protected static $attrs = array(

         // 1 to 1 reference
        'book_fav' => array('model'=>'Purekid\Mongodm\Test\Model\Book','type'=> Model::DATA_TYPE_REFERENCE),
         // 1 to many references
        'books' => array('model'=>'Purekid\Mongodm\Test\Model\Book','type'=> Model::DATA_TYPE_REFERENCES),
        // you can define default value for attribute
        'age' => array('default'=>16,'type'=> Model::DATA_TYPE_INTEGER),
        'money' => array('default'=>20.0,'type'=> Model::DATA_TYPE_DOUBLE),
        'hobbies' => array('default'=>array('love'),'type'=> Model::DATA_TYPE_ARRAY),
        'born_time' => array('type'=> Model::DATA_TYPE_TIMESTAMP),
        'family'=>array('type'=> Model::DATA_TYPE_OBJECT),
        'pet_fav' => array('model'=>'Purekid\Mongodm\Test\Model\Pet','type'=> Model::DATA_TYPE_EMBED),
        'pets' => array('model'=>'Purekid\Mongodm\Test\Model\Pet','type'=> Model::DATA_TYPE_EMBEDS),


    public function setFirstName($name) {
        $name = ucfirst(strtolower($name));
        $this->__setter('firstName', $name);

    public function getLastName($name) {
        $name = $this->__getter('name');
        return strtoupper($name);


Types supported for model attributes

    'mixed',  // mixed type 
    'reference',  // 1 : 1 reference
    'references', // 1 : many references
    'int',  // alias of 'integer'
    'double',     // float 
    'timestamp',  // store as MongoTimestamp in Mongodb
    'date',  // store as DateTime
    'boolean',    // true or false

const DATA_TYPE_ARRAY      = 'array';

const DATA_TYPE_BOOL       = 'bool';
const DATA_TYPE_BOOLEAN    = 'boolean';

const DATA_TYPE_DATE       = 'date';

const DATA_TYPE_DBL        = 'dbl';
const DATA_TYPE_DOUBLE     = 'double';
const DATA_TYPE_FLT        = 'flt';
const DATA_TYPE_FLOAT      = 'float';

const DATA_TYPE_EMBED      = 'embed';
const DATA_TYPE_EMBEDS     = 'embeds';

const DATA_TYPE_INT        = 'int';
const DATA_TYPE_INTEGER    = 'integer';

const DATA_TYPE_MIXED      = 'mixed';

const DATA_TYPE_REFERENCE  = 'reference';
const DATA_TYPE_REFERENCES = 'references';

const DATA_TYPE_STR        = 'str';
const DATA_TYPE_STRING     = 'string';

const DATA_TYPE_TIMESTAMP  = 'timestamp';

const DATA_TYPE_OBJ        = 'obj';
const DATA_TYPE_OBJECT     = 'object';

If you put a object instance into a Model attribute and this attribute is undefined in $attrs of Model class,the data of attribute will be omitted when Model saving.

    $object = new \stdClass();  
    $object->name = 'ooobject';

$user = new User();
$user->name = 'michael';
$user->myobject = $object;    // this attribute will be omitted when saving to DB 

Model CRUD


    $user = new User();
    $user->name = "Michael";
    $user->age = 18;

Create with initial value

    $user = new User( array('name'=>"John") );
    $user->age = 20;

Create using set method

    $user->setLastName('Jones'); // Alias of $user->lastName = 'Jones';
    $user->setFirstName('John'); // Implements setFirstName() method

Set and get values

You can set/get values via variable

$user->name = "John"
or by method

Set using variable or method ```php // no "set" method exists $user->lastName = 'Jones'; $user->setLastName('Jones');

// "set" method exists implements setFirstName()
$user->firstName = 'jOhn'; // "John"
$user->setFirstName('jOhn'); // "John"
Get using variable or method
    // "get" method exists implements getLastName()
    print $user->lastName; // "JONES"
    print $user->getLastName(); // "JONES"

// no "get" method
print $user->firstName; // "John"
print $user->setFirstName('John'); // "John"


    $user->age = 19;

Update attributes by array

    $user->update( array('age'=>18,'hobbies'=>array('music','game') ) ); 
Unset attributes
    $user->unset( array('age','hobbies') );

Retrieve single record

    $user = User::one( array('name'=>"michael" ) );

retrieve one record by MongoId

    $id = "517c850641da6da0ab000004";
    $id = new \MongoId('517c850641da6da0ab000004'); //another way
    $user = User::id( $id );

Retrieve records

Retrieve records that name is 'Michael' and acount of owned books equals 2

    $params = array( 'name'=>'Michael','books'=>array('$size'=>2) );
    $users = User::find($params);     // $users is instance of Collection
    echo $users->count();

Retrieve all records

    $users = User::all();

Count records

    $count = User::count(array('age'=>16));

Delete record

    $user = User::one();

Relationship - Reference

Lazyload a 1:1 relationship record

    $book = new Book();
    $book->name = "My Love";
    $book->price = 15;

// !!!remember you must save book before!!!
$user->book_fav = $book;

// now you can do this
$user = User::one( array('name'=>"michael" ) );
echo $user->book_fav->name;

Lazyload 1:many relationship records

    $user = User::one();
    $id = $user->getId();

$book1 = new Book();
$book1->name = "book1";

$book2 = new Book();
$book2->name = "book2";

$user->books = array($book1,$book2);
//also you can
$user->books = Collection::make(array($book1,$book2));

//somewhere , load these books
$user = User::id($id);
$books = $user->books;      // $books is a instance of Collection

Relationship - Embed

Single Embed

    $pet = new Pet();
    $pet->name = "putty";

$user->pet_fav = $pet;

// now you can do this
$user = User::one( array('name'=>"michael" ) );
echo $user->pet_fav->name;


    $user = User::one();
    $id = $user->getId();

$pet_dog = new Pet();
$pet_dog->name = "puppy";

$pet_cat = new Pet();
$pet_cat->name = "kitty";

$user->pets = array($pet_cat,$pet_dog);
//also you can
$user->pets = Collection::make(array($pet_cat,$pet_dog));

$user = User::id($id);
$pets = $user->pets;     


$users is instance of Collection

    $users = User::find(  array( 'name'=>'Michael','books'=>array('$size'=>2) ) );    
    $users_other = User::find(  array( 'name'=>'John','books'=>array('$size'=>2) ) );   
    $users->save() ;  // foreach($users as $user) { $user->save(); }
    $users->delete() ;  // foreach($users as $user) { $user->delete(); }
foreach($users as $user) { }
// OR use Closure 




//sort by age desc $users->sortBy(function($user){ return $user->age; });

//sort by name asc
    return $user->name;
} , true);

//reverse collection items

Slice and Take

Map ```php
$func = function($user){ if( $user->age >= 18 ){ $user->is_adult = true; } return $user; };
    $func = function($user){
                if( $user->age >= 18 ){
                    return true;

$adults = $users->filter($func); // $adults is a new collection

Determine a record exists in the collection by object instance
$john = User::one(array("name"=>"John"));

Determine a record exists in the collection by numeric index    

Determine a record exists in the collection by MongoID

Get a record by numeric index
Get a record by MongoID
Remove a record by numeric index
Remove a record by MongoID
Add a single record to collection
    $bob = new User( array("name"=>"Bob"));
Add records to collection ```php
$bob = new User( array("name"=>"Bob")); $bob->save(); $lisa = new User( array("name"=>"Lisa")); $lisa->save();
$users->add( array($bob,$lisa) ); 
Merge two collection 
    $users->add($users_other);  // the collection $users_other appends to end of $users 

Export data to a array



Define multilevel inheritable models:

    use Purekid\Mongodm\Model;
    namespace Demo;

class Human extends Model{

    static $collection = "human";

    protected static $attrs = array(
        'name' => array('default'=>'anonym','type'=>'string'),
        'age' => array('type'=>'integer'),
        'gender' => array('type'=>'string'),
        'dad' =>  array('type'=>'reference','model'=>'Demo\Human'),
        'mum' =>  array('type'=>'reference','model'=>'Demo\Human'),
        'friends' => array('type'=>'references','model'=>'Demo\Human'),


class Student extends Human{

    protected static $attrs = array(
        'grade' => array('type'=>'string'),
        'classmates' => array('type'=>'references','model'=>'Demo\Student'),



    $bob = new Student( array('name'=>'Bob','age'=> 17 ,'gender'=>'male' ) );

$john = new Student( array('name'=>'John','age'=> 16 ,'gender'=>'male' ) );

$lily = new Student( array('name'=>'Lily','age'=> 16 ,'gender'=>'female' ) );

$lisa = new Human( array('name'=>'Lisa','age'=>41 ,'gender'=>'female' ) );

$david = new Human( array('name'=>'David','age'=>42 ,'gender'=>'male') );

$bob->dad = $david;
$bob->mum = $lisa;
$bob->classmates = array( $john, $lily );

Retrieve and check value:

    $bob = Student::one( array("name"=>"Bob") );

echo $bob->dad->name;    // David

$classmates = $bob->classmates;

echo $classmates->count(); // 2

var_dump($classmates->get(0)); // john  

Retrieve subclass

Retrieve all Human records , queries without 'type' because of it's a toplevel class.

    $humans = Human::all();
Retrieve all Student records , queries with { "type":"Student" } because of it's a subclass.

    $students = Student::all();

Retrieve subclass without

To retrieve a record without the

criteria (i.e.
{ "_type":"Student" }
) set:
class Student extends \Purekid\Mongodm\Model
    protected static $useType = false;

protected static $collection = 'Student';


Make sure to set a collection otherwise you will get results with every `type`._

Other static methods in Model

    User::drop() // Drop collection 
    User::ensureIndex()  // Add index for collection

Model Hooks

The following hooks are available:


Executed after the constructor has finished


Executed before saving a new record


Executed after saving a new record


Executed before saving an existing record


Executed after saving an existing record


Executed before saving a record


Executed after saving a record


Executed before deleting a record


Executed after deleting a record

Special thanks to

mikelbring Paul Hrimiuc

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.