You are browsing a version that is no longer maintained.

Embedded Mapping

This chapter explains how embedded documents are mapped in Doctrine.

Embed One

Embed a single document:

1<?php /** @Document */ class User { // ... /** @EmbedOne(targetDocument=Address::class) */ private $address; // ... } /** @EmbeddedDocument */ class Address { /** @Field(type="string") */ private $street; // ... }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Embed Many

Embed many documents:

1<?php use Doctrine\Common\Collections\ArrayCollection; /** @Document */ class User { // ... /** @EmbedMany(targetDocument=Phonenumber::class) */ private $phoneNumbers; // ... public function __construct() { $this->phoneNumbers = new ArrayCollection(); } } /** @EmbeddedDocument */ class PhoneNumber { /** @Field(type="string") */ private $number; // ... }
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

Mixing Document Types

If you want to store different types of embedded documents in the same field, you can simply omit the targetDocument option:

1<?php use Doctrine\Common\Collections\ArrayCollection; /** @Document */ class User { // .. /** @EmbedMany */ private $tasks; // ... public function __construct() { $this->tasks = new ArrayCollection(); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Now the $tasks property can store any type of document! The class name will be automatically stored in a field named _doctrine_class_name within the embedded document. The field name can be customized with the discriminatorField option:

1<?php use Doctrine\Common\Collections\ArrayCollection; /** @Document */ class User { // .. /** * @EmbedMany(discriminatorField="type") */ private $tasks; // ... public function __construct() { $this->tasks = new ArrayCollection(); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

You can also specify a discriminator map to avoid storing the FQCN in each embedded document:

1<?php use Doctrine\Common\Collections\ArrayCollection; /** @Document */ class User { // .. /** * @EmbedMany( * discriminatorMap={ * "download"=DownloadTask::class, * "build"=BuildTask::class * } * ) */ private $tasks; // ... public function __construct() { $this->tasks = new ArrayCollection(); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

If you have embedded documents without a discriminator value that need to be treated correctly you can optionally specify a default value for the discriminator:

1<?php /** @Document */ class User { // .. /** * @EmbedMany( * discriminatorMap={ * "download"=DownloadTask::class, * "build"=BuildTask::class * }, * defaultDiscriminatorValue="download" * ) */ private $tasks = []; // ... }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Cascading Operations

All operations on embedded documents are automatically cascaded. This is because embedded documents are part of their parent document and cannot exist without those by nature.