Skip to content

Commit 08b9e17

Browse files
authored
Merge pull request #1807 from alcaeus/use-more-specific-id-mapping
[2.0] Separate ID mapping from fields in XML driver
2 parents fd73454 + af24dbc commit 08b9e17

14 files changed

+64
-41
lines changed

docs/en/reference/basic-mapping.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Here is an example:
234234
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
235235
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
236236
<document name="Documents\User">
237-
<field fieldName="id" id="true" />
237+
<id />
238238
</document>
239239
</doctrine-mongo-mapping>
240240
@@ -278,7 +278,7 @@ Here is an example how to manually set a string identifier for your documents:
278278
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
279279
280280
<document name="MyPersistentClass">
281-
<field name="id" id="true" strategy="NONE" type="string" />
281+
<id strategy="NONE" type="string" />
282282
</document>
283283
</doctrine-mongo-mapping>
284284
@@ -337,9 +337,9 @@ as an option for the ``CUSTOM`` strategy:
337337
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
338338
339339
<document name="MyPersistentClass">
340-
<field name="id" id="true" strategy="CUSTOM" type="string">
341-
<id-generator-option name="class" value="Vendor\Specific\Generator" />
342-
</field>
340+
<id strategy="CUSTOM" type="string">
341+
<generator-option name="class" value="Vendor\Specific\Generator" />
342+
</id>
343343
</document>
344344
</doctrine-mongo-mapping>
345345
@@ -380,7 +380,7 @@ Example:
380380
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
381381
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
382382
<document name="Documents\User">
383-
<field fieldName="id" id="true" />
383+
<id />
384384
<field fieldName="username" type="string" />
385385
</document>
386386
</doctrine-mongo-mapping>

docs/en/reference/capped-collections.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ the ``@Document`` annotation:
4747
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
4848
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
4949
<document name="Documents\Category" collection="collname" capped-collection="true" capped-collection-size="100000" capped-collection-max="1000">
50-
<field fieldName="id" id="true" />
50+
<id />
5151
<field fieldName="name" type="string" />
5252
</document>
5353
</doctrine-mongo-mapping>

docs/en/reference/xml-mapping.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ of several common elements:
109109
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
110110
111111
<document name="Documents\User" db="documents" collection="users">
112-
<field fieldName="id" id="true" />
112+
<id />
113113
<field fieldName="username" name="login" type="string" />
114114
<field fieldName="email" type="string" unique="true" order="desc" />
115115
<field fieldName="createdAt" type="date" />
@@ -156,7 +156,7 @@ Lock
156156
^^^^
157157

158158
The field with the ``lock`` attribute will be used to store lock information for :ref:`pessimistic locking <transactions_and_concurrency_pessimistic_locking>`.
159-
This is only compatible with the ``int`` field type, and cannot be combined with ``id="true"``.
159+
This is only compatible with the ``int`` field type.
160160

161161
.. code-block:: xml
162162
@@ -170,7 +170,7 @@ Version
170170
^^^^^^^
171171

172172
The field with the ``version`` attribute will be used to store version information for :ref:`optimistic locking <transactions_and_concurrency_optimistic_locking>`.
173-
This is only compatible with ``int`` and ``date`` field types, and cannot be combined with ``id="true"``.
173+
This is only compatible with ``int`` and ``date`` field types.
174174

175175
.. code-block:: xml
176176

docs/en/tutorials/getting-started.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ You can provide your mapping information in Annotations or XML:
114114
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
115115
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
116116
<document name="Documents\User">
117-
<field fieldName="id" id="true" />
117+
<id />
118118
<field fieldName="name" type="string" />
119119
<field fieldName="email" type="string" />
120120
<reference-many fieldName="posts" targetDocument="Documents\BlogPost">
@@ -131,7 +131,7 @@ You can provide your mapping information in Annotations or XML:
131131
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
132132
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
133133
<document name="Documents\BlogPost">
134-
<field fieldName="id" id="true" />
134+
<id />
135135
<field fieldName="title" type="string" />
136136
<field fieldName="body" type="string" />
137137
<field fieldName="createdAt" type="date" />

doctrine-mongo-mapping.xsd

+10-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
<xs:complexType name="document">
3939
<xs:sequence>
40+
<xs:element name="id" type="odm:id" minOccurs="0" maxOccurs="1"/>
4041
<xs:element name="field" type="odm:field" minOccurs="0" maxOccurs="unbounded"/>
4142
<xs:element name="embed-one" type="odm:embed-one" minOccurs="0" maxOccurs="unbounded"/>
4243
<xs:element name="embed-many" type="odm:embed-many" minOccurs="0" maxOccurs="unbounded"/>
@@ -72,10 +73,6 @@
7273
</xs:complexType>
7374

7475
<xs:complexType name="field">
75-
<xs:sequence>
76-
<xs:element name="id-generator-option" type="odm:id-generator-option" minOccurs="0" maxOccurs="unbounded" />
77-
</xs:sequence>
78-
<xs:attribute name="id" type="xs:boolean" default="false" />
7976
<xs:attribute name="name" type="xs:NMTOKEN" />
8077
<xs:attribute name="type" type="xs:NMTOKEN" />
8178
<xs:attribute name="strategy" type="xs:NMTOKEN" default="set" />
@@ -97,6 +94,15 @@
9794
<xs:attribute name="unique" type="xs:boolean" />
9895
</xs:complexType>
9996

97+
<xs:complexType name="id">
98+
<xs:sequence>
99+
<xs:element name="generator-option" type="odm:id-generator-option" minOccurs="0" maxOccurs="unbounded" />
100+
</xs:sequence>
101+
<xs:attribute name="type" type="xs:NMTOKEN" />
102+
<xs:attribute name="strategy" type="xs:NMTOKEN" default="auto" />
103+
<xs:attribute name="fieldName" type="xs:NMTOKEN" default="id" />
104+
</xs:complexType>
105+
100106
<xs:complexType name="id-generator-option">
101107
<xs:attribute name="name" type="xs:NMTOKEN" use="required"/>
102108
<xs:attribute name="value" type="xs:string" use="required"/>

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

+30-13
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,36 @@ public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Ma
124124
if (isset($xmlRoot->{'read-preference'})) {
125125
$class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'}));
126126
}
127+
128+
if (isset($xmlRoot->id)) {
129+
$field = $xmlRoot->id;
130+
$mapping = [
131+
'id' => true,
132+
'fieldName' => 'id',
133+
];
134+
135+
$attributes = $field->attributes();
136+
foreach ($attributes as $key => $value) {
137+
$mapping[$key] = (string) $value;
138+
}
139+
140+
if (isset($mapping['strategy'])) {
141+
$mapping['options'] = [];
142+
if (isset($field->{'generator-option'})) {
143+
foreach ($field->{'generator-option'} as $generatorOptions) {
144+
$attributesGenerator = iterator_to_array($generatorOptions->attributes());
145+
if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) {
146+
continue;
147+
}
148+
149+
$mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
150+
}
151+
}
152+
}
153+
154+
$this->addFieldMapping($class, $mapping);
155+
}
156+
127157
if (isset($xmlRoot->field)) {
128158
foreach ($xmlRoot->field as $field) {
129159
$mapping = [];
@@ -137,19 +167,6 @@ public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Ma
137167

138168
$mapping[$key] = ($mapping[$key] === 'true');
139169
}
140-
if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
141-
$mapping['options'] = [];
142-
if (isset($field->{'id-generator-option'})) {
143-
foreach ($field->{'id-generator-option'} as $generatorOptions) {
144-
$attributesGenerator = iterator_to_array($generatorOptions->attributes());
145-
if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) {
146-
continue;
147-
}
148-
149-
$mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
150-
}
151-
}
152-
}
153170

154171
if (isset($attributes['not-saved'])) {
155172
$mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');

tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH774/Doctrine.ODM.MongoDB.Tests.GH774AbstractThread.dcm.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
55
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
66

7-
<mapped-superclass name="Doctrine\ODM\MongoDB\Tests\GH774AbstractThread" collection="threads" customId="true">
7+
<mapped-superclass name="Doctrine\ODM\MongoDB\Tests\GH774AbstractThread" collection="threads">
88

9-
<field name="id" type="string" id="true" strategy="NONE" />
9+
<id type="string" strategy="NONE" />
1010

1111
<field name="permalink" type="string" />
1212

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.InvalidPartialFilterDocument.dcm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
77

88
<document name="TestDocuments\InvalidPartialFilterDocument" db="documents" collection="partialFilterDocument">
9-
<field name="id" id="true" />
9+
<id />
1010
<indexes>
1111
<index>
1212
<key name="fieldA" order="asc" />

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.PartialFilterDocument.dcm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
77

88
<document name="TestDocuments\PartialFilterDocument" db="documents" collection="partialFilterDocument">
9-
<field name="id" id="true" />
9+
<id />
1010
<indexes>
1111
<index>
1212
<key name="fieldA" order="asc" />

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.PrimedCollectionDocument.dcm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
77

88
<document name="TestDocuments\PrimedCollectionDocument" collection="primed_collection">
9-
<field name="id" id="true" />
9+
<id />
1010

1111
<reference-many target-document="TestDocuments\PrimedCollectionDocument" field="references" />
1212
<reference-many target-document="TestDocuments\PrimedCollectionDocument" field="inverseMappedBy" mapped-by="references">

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.User.dcm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
77

88
<document name="TestDocuments\User" db="documents" collection="users">
9-
<field name="id" id="true" />
9+
<id />
1010
<field name="username" type="string" unique="true" sparse="true"/>
1111
<field name="createdAt" type="date" />
1212
<field name="tags" type="collection" />

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.UserCustomIdGenerator.dcm.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
77

88
<document name="TestDocuments\UserCustomIdGenerator" db="documents" collection="users">
9-
<field name="id" id="true" strategy="custom">
10-
<id-generator-option name="class" value="TestDocuments\CustomIdGenerator"/>
11-
<id-generator-option name="someOption" value="some-option"/>
12-
</field>
9+
<id strategy="custom">
10+
<generator-option name="class" value="TestDocuments\CustomIdGenerator"/>
11+
<generator-option name="someOption" value="some-option"/>
12+
</id>
1313
</document>
1414
</doctrine-mongo-mapping>

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/TestDocuments.UserNonStringOptions.dcm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
77

88
<document name="TestDocuments\UserNonStringOptions">
9-
<field name="id" id="true" />
9+
<id />
1010
<reference-one target-document="Documents\Profile" field="profile" store-as="id" orphan-removal="true" />
1111
<reference-many target-document="Documents\Group" field="groups" orphan-removal="" limit="0" skip="2" />
1212
</document>

tests/Doctrine/ODM/MongoDB/Tests/Mapping/xml/Doctrine.ODM.MongoDB.Tests.Mapping.AbstractMappingDriverUser.dcm.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</tag-set>
2121
<tag-set />
2222
</read-preference>
23-
<field fieldName="id" id="true" />
23+
<id fieldName="id" />
2424
<field fieldName="version" version="true" type="int" />
2525
<field fieldName="lock" lock="true" type="int" />
2626
<field fieldName="name" name="username" type="string" />

0 commit comments

Comments
 (0)