Skip to content


Subversion checkout URL

You can clone with
Download ZIP
executable file 302 lines (222 sloc) 8.364 kB

Getting Started

Doctrine is a project that aims to handle the persistence of your domain model in a non-interfering way. Non-relational or no-sql databases like MongoDB give you flexibility of building data store around your object model and not vise versa. You can read more on the initial configuration and setup in :doc:`Introduction to MongoDB Object Document Mapper <../reference/introduction>`. This section will give you a basic overview of what could be accomplished using Doctrine MongoDB ODM.

Example Model: Simple Blog

To create the simplest example, let’s assume the following in a simple blog web application:

  • Blog has a user.
  • Blog user can make blog posts

A first prototype

For the above mentioned example, something as simple as this could be modeled with plain PHP classes. First define the User document:


namespace Documents;

class User
    private $name;
    private $email;
    private $posts = array();

    // ...

Now define the BlogPost document:


namespace Documents;

class BlogPost
    private $title;
    private $body;
    private $createdAt;

    // ...

Persistent Models

To make the above classes persistent, all we need to do is provide Doctrine with some mapping information so that it knows how to consume the objects and persist them to the database.

You can provide your mapping information in Annotations, XML, or YAML:

That’s it, we have our models, and we can save and retrieve them. Now all we need to do is to properly instantiate the DocumentManager instance. Read more about setting up the Doctrine MongoDB ODM in the :doc:`Introduction to MongoDB Object Document Mapper <../reference/introduction>`:


use Doctrine\MongoDB\Connection;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;


$config = new Configuration();

$dm = DocumentManager::create(new Connection(), $config);


Here is how you would use your models now:


// ...

// create user
$user = new User();
$user->setName('Bulat S.');

// tell Doctrine 2 to save $user on the next flush()

// create blog post
$post = new BlogPost();
$post->setTitle('My First Blog Post');
$post->setBody('MongoDB + Doctrine 2 ODM = awesomeness!');
$post->setCreatedAt(new DateTime());


// store everything to MongoDB


Note that you do not need to explicitly call persist on the $post because the operation will cascade on to the reference automatically.

Now if you did everything correctly, you should have those two objects stored in MongoDB in correct collections and databases. You can use the php-mongodb-admin project, hosted on github to look at your BlogPost collection, where you will see only one document:

    [_id] => 4bec5869fdc212081d000000
    [title] => My First Blog Post
    [body] => MongoDB + Doctrine 2 ODM = awesomeness!
    [createdAt] => MongoDate Object
            [sec] => 1273723200
            [usec] => 0

And the User collection would consist of the following:

    [_id] => 4bec5869fdc212081d010000
    [name] => Bulat S.
    [email] =>
    [posts] => Array
            [0] => Array
                    [$ref] => blog_posts
                    [$id] => 4bec5869fdc212081d000000
                    [$db] => test_database

You can retrieve the user later by its identifier:


// ...

$userId = '....';
$user = $dm->find('User', $userId);

Or you can find the user by name even:


$user = $dm->getRepository('User')->findOneByName('Bulat S.');

If you want to iterate over the posts the user references it is as easy as the following:


$posts = $dm->getPosts();
foreach ($posts as $post) {

You will notice that working with objects is nothing magical and you only have access to the properties, getters and setters that you have defined yourself so the semantics are very clear. You can continue reading about the MongoDB in the :doc:`Introduction to MongoDB Object Document Mapper <../reference/introduction>`.

Jump to Line
Something went wrong with that request. Please try again.