You are browsing a version that is no longer maintained.

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.
1<?php $driver->setFileExtension('.yml');
2
3

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:

1<?php // $config instanceof Doctrine\ODM\MongoDB\Configuration $driver = new YamlDriver(array('/path/to/files')); $config->setMetadataDriverImpl($driver);
2
3
4
5

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:

1<?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
2
3
4
5
6
7

Example

As a quick start, here is a small example document that makes use of several common elements:

1# 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

Be aware that class-names specified in the YAML files should be fully qualified.

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

Lock

The field with the lock property will be used to store lock information for pessimistic locking. This is only compatible with the int field type, and cannot be combined with id: true.

1lock: type: int lock: true
2
3

Version

The field with the version property will be used to store version information for optimistic locking. This is only compatible with int and date field types, and cannot be combined with id: true.

1version: type: int version: true
2
3

By default, Doctrine ODM updates embed-many and reference-many collections in separate write operations, which do not bump the document version. Users employing document versioning are encouraged to use the atomicSet or atomicSetArray strategies for such collections, which will ensure that collections are updated in the same write operation as the versioned parent document.