You are browsing a version that has not yet been released. |
XML Mapping
The XML mapping driver enables you to provide the ODM metadata in form of XML documents.
The XML driver is backed by an XML Schema document that describes the structure of a mapping document. The most recent version of the XML Schema document is available online at http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd. The most convenient way to work with XML mapping files is to use an IDE/editor that can provide code-completion based on such an XML Schema document. The following is an outline of a XML mapping document with the proper xmlns/xsi setup for the latest code in trunk.
1 <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
...
</doctrine-mongo-mapping>
2
3
4
5
6
7
8
The XML 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 XML 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.xml" 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.
It is recommended to put all XML mapping documents in a single folder but you can spread the documents over several folders if you want to. In order to tell the XmlDriver where to look for your mapping documents, supply an array of paths as the first argument of the constructor, like this:
Simplified XML Driver
The Symfony project sponsored a driver that simplifies usage of the XML Driver. The changes between the original driver are:
- File Extension is .mongodb-odm.xml
- Filenames are shortened, "MyProjectDocumentsUser" will become User.mongodb-odm.xml
- You can add a global file and add multiple documents in this file.
Configuration of this client works a little bit different:
Example
As a quick start, here is a small example document that makes use of several common elements:
1 // Documents.User.dcm.xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="Documents\User" db="documents" collection="users">
<id />
<field field-name="username" name="login" type="string" />
<field field-name="email" type="string" unique="true" order="desc" />
<field field-name="createdAt" type="date" />
<indexes>
<index unique="true">
<key name="username" order="desc" />
<option name="safe" value="true" />
</index>
</indexes>
<embed-one target-document="Documents\Address" field="address" />
<reference-one target-document="Documents\Profile" field="profile">
<cascade>
<all />
</cascade>
</reference-one>
<embed-many target-document="Documents\Phonenumber" field="phonenumbers" />
<reference-many target-document="Documents\Group" field="groups">
<cascade>
<all />
</cascade>
</reference-many>
<reference-one target-document="Documents\Account" field="account">
<cascade>
<all />
</cascade>
</reference-one>
</document>
</doctrine-mongo-mapping>
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
Be aware that class-names specified in the XML files should be fully qualified.
|
Reference
Lock
The field with the lock
attribute will be used to store lock information for pessimistic locking.
This is only compatible with the int
field type.
Version
The field with the version
attribute will be used to store version information for optimistic locking.
This is only compatible with int
and date
field types.
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.