Embedded Mapping
================
This chapter explains how embedded documents are mapped in
Doctrine.
.. _embed_one:
Embed One
---------
Embed a single document:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
User:
type: document
embedOne:
address:
targetDocument: Address
Address:
type: embeddedDocument
.. _embed_many:
Embed Many
----------
Embed many documents:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
User:
type: document
embedMany:
phonenumbers:
targetDocument: Phonenumber
Phonenumber:
type: embeddedDocument
.. _embed_mixing_document_types:
Mixing Document Types
---------------------
If you want to store different types of embedded documents in the same field,
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 stored in a field named ``_doctrine_class_name`` within
the embedded document. The field name can be customized with the
``discriminatorField`` option:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
embedMany:
tasks:
discriminatorField: type
You can also specify a discriminator map to avoid storing the |FQCN|
in each embedded document:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
embedMany:
tasks:
discriminatorMap:
download: DownloadTask
build: BuildTask
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:
.. configuration-block::
.. code-block:: php
.. code-block:: yaml
embedMany:
tasks:
discriminatorMap:
download: DownloadTask
build: BuildTask
defaultDiscriminatorValue: download
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.
.. |FQCN| raw:: html
FQCN