The easy, powerful and ultrafast ODM for PHP and MongoDB.

Working with objects

You have to create the documents through the create method of the mandango, passing it the document class:

$article = $mandango->create('Model\Article');

Mandango uses setters and getters to modify and access to the document's data.


To initialize the documents you have to create the initialize method, where you can choose the arguments, and pass them as an array creating the documents:

// the initialize method
public function initialize(Author $author)

// creating the document
$author = $mandango->create('Model\Author');
$article = $mandango->create('Model\Article', array($author));


To access the identifier _id of the mongo documents it is used the ->getId() method. It returns null if the document is new:

$id = $article->getId();

New check

To check if a document exists in the database you have to use the ->isNew() method:

$isNew = $article->isNew();


You have to use just the setters and getters:

// setter
$article->setTitle('Mandango rocks!');

// getter
$title = $article->getTitle();



To assign references to one you simply have to assign the referenced document to the reference's setter:

$author = $mandango->create('Mandango\Author');


And to access the referenced objects is in the same way but with the getter:

$author = $article->getAuthor();

If there is no a reference document it just returns null.

The referenced document is queried automatically to the database if it has not been queried before.


To work with references to many it is used a Mandango\ReferenceGroup object, to store the referenced documents.

Adding and deleting referenced documents:

// retrieving the group
$categories = $article->getCategories();

// adding one by one

// adding several at the same time
$categories->add(array($category1, $category2));

// shortcut to add from the document
$article->addCategories($category); // one
$article->addCategories($categories); // several

// remove one by one

// removing several at the same time
$categories->remove(array($category1, $category2));

// shortcut to remove from the document
$article->removeCategories($category); // one
$article->removeCategories($categories); // several

// replacing (delete the existent ones and add the new ones)
$article->getCategories()->replace(array($category2, $category4, $category6));

To work with the references to many the ReferenceGroup has some useful methods:

// retrieving all documents (saved + add - removed)
$categories = $categories->all();

// count the documents
$count = $categories->count();

The ReferenceGroup class implements the Countable and IteratorAggregate interfaces, so you can use them as well:

// saved
foreach ($categories as $category) {
    // ...
foreach ($article->getCategories() as $category) {
    // ...

// count
$count = count($categories);
$count = count($article->getCategories());

The ReferenceGroup has also an extremely useful createQuery method that we will see later of see the queries.


To work with the embeddeds is quite similar to work with the references.



$source = $article->getSource();

To many (many)

To work with the embeddeds many is used the EmbeddedGroup class instead, but it works in the similar way that the ReferenceGroup one:



// ...


The relations can only be accessed, and they return a document or a Mandango\Query object depending on the type:

// one
$phonenumber = $author->getPhonenumber(); // document

// one_many
$articles = $authors->getArticles(); // Mandango\Query

// many_many
$articles = $category->getArticles(); // Mandango\Query

We will see later why a query object is returned instead of an array of documents. A query object is much more useful.

Save and delete

To save and delete Mandango documents you can use the methods ->save() y ->delete() of the documents:

// save

// delete

These methods are not in the embedded documents. You have to save always the documents through the main document.

Fluent interface

A fluent interface is implemented in the mandango documents to be able to work easily with them:

$author = $mandango->create('Model\Author');

$article = $mandango->create('Model\Article');

// fluent interface
$author = $mandango->create('Model\Author')->setName('pablodip')->save();

$article = $mandango->create('Model\Article')
Fork me on GitHub