Embedded Mapping
================
This chapter explains how embedded documents are mapped in
Doctrine.
Embed One
---------
Embed a single document:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
User:
type: document
embedOne:
address:
targetDocument: Address
Embed Many
----------
Embed many documents:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
User:
type: document
embedMany:
phonenumbers:
targetDocument: Phonenumber
Mixing Document Types
---------------------
If you want to store different types of documents in an embedded
document you can simply omit the ``targetDocument`` option:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
embedMany:
tasks: ~
Now the ``$tasks`` property can store any type of document! The
class name will be automatically added for you in a field named
``_doctrine_class_name``.
You can also specify a discriminator map to avoid storing the fully
qualified class name with each embedded document:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
embedMany:
tasks:
discriminatorMap:
download: DownloadTask
build: BuildTask
If you want to store the discriminator value in a field other than
``_doctrine_class_name`` you can use the ``discriminatorField``
option:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
embedMany:
tasks:
discriminatorField: type
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.