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 Introduction to MongoDB Object Document Mapper. 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:
- a Blog has a "user".
- a Blog "user" can make "blog posts"
A first prototype
For the above mentioned example, we start by defining two simple PHP classes:
User
and BlogPost
. The User
class will have a collection of
BlogPost
objects.
Now define the BlogPost
document that contains the title, body and the date
of creation:
Persistent Models
To make the above classes persistent, we need to 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 or XML:
1 <?php
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
#[ODM\Document]
class User
{
public function __construct(
#[ODM\Id]
public ?string $id = null,
#[ODM\Field]
public string $name = '',
#[ODM\Field]
public string $email = '',
#[ODM\ReferenceMany(targetDocument: BlogPost::class, cascade: 'all')]
public Collection $posts = new ArrayCollection(),
) {
}
// ...
}
#[ODM\Document]
class BlogPost
{
public function __construct(
#[ODM\Id]
public ?string $id = null,
#[ODM\Field]
public string $title = '',
#[ODM\Field]
public string $body = '',
#[ODM\Field]
public DateTimeImmutable $createdAt = new DateTimeImmutable(),
) {
}
// ...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
The |
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
Introduction to MongoDB Object Document Mapper:
1 <?php
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
require_once __DIR__ . '/vendor/autoload.php';
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/generated/proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/generated/hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setMetadataDriverImpl(AttributeDriver::create(__DIR__ . '/src'));
$dm = DocumentManager::create(config: $config);
spl_autoload_register($config->getProxyManagerConfiguration()->getProxyAutoloader());
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Usage
Here is how you would use your models now:
1 <?php
// ...
// create user
$user = new User(
name: 'Bulat S.',
email: '[email protected]',
);
// tell Doctrine to save $user on the next flush()
$dm->persist($user);
// create blog post
$post = new BlogPost(
title: 'My First Blog Post',
body: 'MongoDB + Doctrine ODM = awesomeness!',
);
// link the blog post to the user
$user->posts->add($post);
// store everything to MongoDB
$dm->flush();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Note that you do not need to explicitly call persist on the |
After running this code, you should have those two objects stored in the collections "User" and "BlogPost". You can use MongoDB Compass to inspect the contents of your database, where you will see this documents:
// BlogPost collection
{
_id: ObjectId("4bec5869fdc212081d000000"),
title: "My First Blog Post",
body: "MongoDB + Doctrine ODM = awesomeness!",
createdAt: Date("2010-05-13T18:00:00Z")
}
// User collection
{
_id: ObjectId("4bec5869fdc212081d010000"),
name: "Bulat S.",
email: "[email protected]",
posts: [
DBRef("BlogPost", "4bec5869fdc212081d000000")
],
}
You can retrieve the user later by its identifier:
Or you can find the user by name even:
If you want to iterate over the posts the user references it is as easy as the following:
When retrieved from the database, |
You will notice that working with objects is nothing magical and you only have access to the properties and methods that you have defined yourself. You can continue reading Introduction to MongoDB Object Document Mapper.