mongodb-odm-docs-dash/Doctrine ODM.docset/Contents/Resources/Documents/_sources/reference/yml-mapping.rst.txt

212 lines
5.7 KiB
ReStructuredText

YAML Mapping
============
The YAML mapping driver enables you to provide the ODM metadata in
form of YAML documents.
The YAML mapping document of a class is loaded on-demand the first
time it is requested and subsequently stored in the metadata cache.
In order to work, this requires certain conventions:
-
Each document/mapped superclass must get its own dedicated YAML
mapping document.
-
The name of the mapping document must consist of the fully
qualified name of the class, where namespace separators are
replaced by dots (.).
-
All mapping documents should get the extension ".dcm.yml" to
identify it as a Doctrine mapping file. This is more of a
convention and you are not forced to do this. You can change the
file extension easily enough.
-
.. code-block:: php
<?php
$driver->setFileExtension('.yml');
It is recommended to put all YAML mapping documents in a single
folder but you can spread the documents over several folders if you
want to. In order to tell the YamlDriver where to look for your
mapping documents, supply an array of paths as the first argument
of the constructor, like this:
.. code-block:: php
<?php
// $config instanceof Doctrine\ODM\MongoDB\Configuration
$driver = new YamlDriver(array('/path/to/files'));
$config->setMetadataDriverImpl($driver);
Simplified YAML Driver
~~~~~~~~~~~~~~~~~~~~~~
The Symfony project sponsored a driver that simplifies usage of the YAML Driver.
The changes between the original driver are:
1. File Extension is .mongodb-odm.yml
2. Filenames are shortened, "MyProject\\Documents\\User" will become User.mongodb-odm.yml
3. You can add a global file and add multiple documents in this file.
Configuration of this client works a little bit different:
.. code-block:: php
<?php
$namespaces = array(
'/path/to/files1' => 'MyProject\Documents',
'/path/to/files2' => 'OtherProject\Documents'
);
$driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\SimplifiedYamlDriver($namespaces);
$driver->setGlobalBasename('global'); // global.mongodb-odm.yml
Example
-------
As a quick start, here is a small example document that makes use
of several common elements:
.. code-block:: yaml
# Documents.User.dcm.yml
Documents\User:
db: documents
collection: user
fields:
id:
id: true
username:
name: login
type: string
email:
unique:
order: desc
createdAt:
type: date
indexes:
index1:
keys:
username: desc
options:
unique: true
dropDups: true
safe: true
embedOne:
address:
targetDocument: Documents\Address
embedMany:
phonenumbers:
targetDocument: Documents\Phonenumber
referenceOne:
profile:
targetDocument: Documents\Profile
cascade: all
account:
targetDocument: Documents\Account
cascade: all
referenceMany:
groups:
targetDocument: Documents\Group
cascade: all
# Alternative syntax for the exact same example
# (allows custom key name for embedded document and reference).
Documents\User:
db: documents
collection: user
fields:
id:
id: true
username:
name: login
type: string
email:
unique:
order: desc
createdAt:
type: date
address:
embedded: true
type: one
targetDocument: Documents\Address
phonenumbers:
embedded: true
type: many
targetDocument: Documents\Phonenumber
profile:
reference: true
type: one
targetDocument: Documents\Profile
cascade: all
account:
reference: true
type: one
targetDocument: Documents\Account
cascade: all
groups:
reference: true
type: many
targetDocument: Documents\Group
cascade: all
indexes:
index1:
keys:
username: desc
options:
unique: true
dropDups: true
safe: true
Be aware that class-names specified in the YAML files should be fully qualified.
.. note::
The ``name`` property is an optional setting to change name of the field
**in the database**. Specifying it is optional and defaults to the name
of mapped field.
Reference
---------
.. _yml_reference_lock:
Lock
^^^^
The field with the ``lock`` property will be used to store lock information for :ref:`pessimistic locking <transactions_and_concurrency_pessimistic_locking>`.
This is only compatible with the ``int`` field type, and cannot be combined with ``id: true``.
.. code-block:: yaml
lock:
type: int
lock: true
.. _yml_reference_version:
Version
^^^^^^^
The field with the ``version`` property will be used to store version information for :ref:`optimistic locking <transactions_and_concurrency_optimistic_locking>`.
This is only compatible with ``int`` and ``date`` field types, and cannot be combined with ``id: true``.
.. code-block:: yaml
version:
type: int
version: true
By default, Doctrine ODM updates :ref:`embed-many <embed_many>` and
:ref:`reference-many <reference_many>` collections in separate write operations,
which do not bump the document version. Users employing document versioning are
encouraged to use the :ref:`atomicSet <atomic_set>` or
:ref:`atomicSetArray <atomic_set_array>` strategies for such collections, which
will ensure that collections are updated in the same write operation as the
versioned parent document.