The repositories perform general functions of documents.
They are obtained through the getRepository method of the mandango:
$articleRepository = $mandango->getRepository('Model\Article');
$authorRepository = $mandango->getRepository('Model\Author');
Note
The embedded documents don’t have repositories.
The repositories implement the findById and findOneById methods to find documents by id:
// one
$article = $articleRepository->findOneById($id);
// several
$articles = $articleRepository->findById(array($id1, $id2, $id3));
Mandango implements the IdentityMap pattern, so when you find a document and the document has been found already, the same document is returned.
Note
You can use a \MongoId object or a string.
To save documents you can also use the ->save() method of the repositories, which is the one that the save method of the documents uses internally:
// saving a document
$articleRepository->save($article);
// saving several documents
$articleRepository->save(array($article1, $article2));
Note
To insert documents it is used the batchInsert method, and to update the atomic operations one, so both functions are done in a very efficient way.
Tip
Inserting document through the repository directly is useful when you have to insert more than one document. This way all the operation is done in the same batchInsert.
The same logic that with the save method is applied here:
// deleting a document
$articleRepository->delete($article);
// deleting several documents
$articleRepository->delete(array($article1, $article2));
Note
It is also very useful to delete a lot of documents from the repository, because it uses the $in operator.
The repositories have the group method, which is a shortcut to the group method of the repositories:
$result = $repository->group($keys, $initial, $reduce);
$result = $repository->group($keys, $initial, $reduce, $options); // options optional
// the same as
$result = $repository
->getCollection()
->group($keys, $initial, $reduce, $options)
;
The repositories have the distinct method to execute distinct commands easily:
$result = $repository->distinct($field);
$result = $repository->distinct($field, $query); // query optional
// the same as
$result = $repository
->getCollection()
->getMongoDB()
->command(array(
'distinct' => $repository->getCollectionName(),
'key' => $field,
'query' => $query,
))
;
The repositories have the mapReduce method to execute map reduces commands easily:
$result = $repository->mapReduce($map, $reduce, $out);
$result = $repository->mapReduce($map, $reduce, $out, $query, $options); // more args (optionals)
// the same as
$result = $repository
->getCollection()
->getMongoDB()
->command(array_merge($options, array(
'mapreduce' => $repository->getCollectionName(),
'map' => $map,
'reduce' => $reduce,
'out' => $out,
'query' => $query,
)))
;
You can get the mandango connection of the documents from the repository through the ->getConnection() method:
$connection = $articleRepository->getConnection();
You can also obtain the mongo collection to perform operations directly with the ->getConnection() method:
$collection = \Model\Article::getRepository()->getCollection();