2010-11-02 00:03:50 +03:00
|
|
|
YAML Mapping
|
|
|
|
============
|
|
|
|
|
2010-11-01 23:16:12 +03:00
|
|
|
The YAML mapping driver enables you to provide the ORM 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 entity/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.
|
|
|
|
|
|
|
|
-
|
|
|
|
|
2010-12-03 22:13:10 +03:00
|
|
|
.. code-block:: php
|
2010-11-01 23:16:12 +03:00
|
|
|
|
|
|
|
<?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:
|
|
|
|
|
2010-12-03 22:13:10 +03:00
|
|
|
.. code-block:: php
|
2010-11-01 23:16:12 +03:00
|
|
|
|
|
|
|
<?php
|
|
|
|
// $config instanceof Doctrine\ORM\Configuration
|
|
|
|
$driver = new YamlDriver(array('/path/to/files'));
|
|
|
|
$config->setMetadataDriverImpl($driver);
|
|
|
|
|
2011-10-15 11:47:56 +04:00
|
|
|
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 .orm.yml
|
|
|
|
2. Filenames are shortened, "MyProject\Entities\User" will become User.orm.yml
|
|
|
|
3. You can add a global file and add multiple entities in this file.
|
|
|
|
|
|
|
|
Configuration of this client works a little bit different:
|
|
|
|
|
|
|
|
.. code-block:: php
|
|
|
|
|
|
|
|
<?php
|
|
|
|
$namespaces = array(
|
|
|
|
'MyProject\Entities' => '/path/to/files1',
|
|
|
|
'OtherProject\Entities' => '/path/to/files2'
|
|
|
|
);
|
|
|
|
$driver = new \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver($namespaces);
|
|
|
|
$driver->setGlobalBasename('global'); // global.orm.yml
|
|
|
|
|
2010-11-01 23:16:12 +03:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
As a quick start, here is a small example document that makes use
|
|
|
|
of several common elements:
|
|
|
|
|
2010-11-02 00:03:50 +03:00
|
|
|
.. code-block:: yaml
|
2010-11-01 23:16:12 +03:00
|
|
|
|
|
|
|
# Doctrine.Tests.ORM.Mapping.User.dcm.yml
|
|
|
|
Doctrine\Tests\ORM\Mapping\User:
|
|
|
|
type: entity
|
|
|
|
table: cms_users
|
|
|
|
id:
|
|
|
|
id:
|
|
|
|
type: integer
|
|
|
|
generator:
|
|
|
|
strategy: AUTO
|
|
|
|
fields:
|
|
|
|
name:
|
|
|
|
type: string
|
|
|
|
length: 50
|
|
|
|
oneToOne:
|
|
|
|
address:
|
|
|
|
targetEntity: Address
|
|
|
|
joinColumn:
|
|
|
|
name: address_id
|
|
|
|
referencedColumnName: id
|
|
|
|
oneToMany:
|
|
|
|
phonenumbers:
|
|
|
|
targetEntity: Phonenumber
|
|
|
|
mappedBy: user
|
2011-03-28 00:23:37 +04:00
|
|
|
cascade: ["persist", "merge"]
|
2010-11-01 23:16:12 +03:00
|
|
|
manyToMany:
|
|
|
|
groups:
|
|
|
|
targetEntity: Group
|
|
|
|
joinTable:
|
|
|
|
name: cms_users_groups
|
|
|
|
joinColumns:
|
|
|
|
user_id:
|
|
|
|
referencedColumnName: id
|
|
|
|
inverseJoinColumns:
|
|
|
|
group_id:
|
|
|
|
referencedColumnName: id
|
|
|
|
lifecycleCallbacks:
|
|
|
|
prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersistToo ]
|
|
|
|
postPersist: [ doStuffOnPostPersist ]
|
|
|
|
|
|
|
|
Be aware that class-names specified in the YAML files should be
|
|
|
|
fully qualified.
|
|
|
|
|
|
|
|
|