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:

<?php/** @Document */class User{    // ...    /** @EmbedOne(targetDocument="Address") */    private $address;    // ...}/** @EmbeddedDocument */class Address{    // ...}

Embed Many

Embed many documents:

<?php/** @Document */class User{    // ...    /** @EmbedMany(targetDocument="Phonenumber") */    private $phonenumbers = array();    // ...}/** @EmbeddedDocument */class Phonenumber{    // ...}

Mixing Document Types

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

<?php/** @Document */class User{    // ..    /** @EmbedMany */    private $tasks = array();    // ...}

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:

<?php/** @Document */class User{    // ..    /**     * @EmbedMany(discriminatorField="type")     */    private $tasks = array();    // ...}

You can also specify a discriminator map to avoid storing the fully qualified class name in each embedded document:

<?php/** @Document */class User{    // ..    /**     * @EmbedMany(     *   discriminatorMap={     *     "download"="DownloadTask",     *     "build"="BuildTask"     *   }     * )     */    private $tasks = array();    // ...}

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:

<?php/** @Document */class User{    // ..    /**     * @EmbedMany(     *   discriminatorMap={     *     "download"="DownloadTask",     *     "build"="BuildTask"     *   },     *   defaultDiscriminatorValue="download"     * )     */    private $tasks = array();    // ...}

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.