Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] Separate ID mapping from fields in XML driver #1807

Merged
merged 1 commit into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Separate ID mapping from fields in XML driver
  • Loading branch information
alcaeus committed Jun 5, 2018
commit af24dbcf93f9aab3c57bc86f0263765739ec5c1e
12 changes: 6 additions & 6 deletions docs/en/reference/basic-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Here is an example:
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">
<field fieldName="id" id="true" />
<id />
</document>
</doctrine-mongo-mapping>

Expand Down Expand Up @@ -278,7 +278,7 @@ Here is an example how to manually set a string identifier for your documents:
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="MyPersistentClass">
<field name="id" id="true" strategy="NONE" type="string" />
<id strategy="NONE" type="string" />
</document>
</doctrine-mongo-mapping>

Expand Down Expand Up @@ -337,9 +337,9 @@ as an option for the ``CUSTOM`` strategy:
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="MyPersistentClass">
<field name="id" id="true" strategy="CUSTOM" type="string">
<id-generator-option name="class" value="Vendor\Specific\Generator" />
</field>
<id strategy="CUSTOM" type="string">
<generator-option name="class" value="Vendor\Specific\Generator" />
</id>
</document>
</doctrine-mongo-mapping>

Expand Down Expand Up @@ -380,7 +380,7 @@ Example:
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">
<field fieldName="id" id="true" />
<id />
<field fieldName="username" type="string" />
</document>
</doctrine-mongo-mapping>
Expand Down
2 changes: 1 addition & 1 deletion docs/en/reference/capped-collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ the ``@Document`` annotation:
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="Documents\Category" collection="collname" capped-collection="true" capped-collection-size="100000" capped-collection-max="1000">
<field fieldName="id" id="true" />
<id />
<field fieldName="name" type="string" />
</document>
</doctrine-mongo-mapping>
Expand Down
6 changes: 3 additions & 3 deletions docs/en/reference/xml-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ of several common elements:
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="Documents\User" db="documents" collection="users">
<field fieldName="id" id="true" />
<id />
<field fieldName="username" name="login" type="string" />
<field fieldName="email" type="string" unique="true" order="desc" />
<field fieldName="createdAt" type="date" />
Expand Down Expand Up @@ -156,7 +156,7 @@ Lock
^^^^

The field with the ``lock`` attribute 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"``.
This is only compatible with the ``int`` field type.

.. code-block:: xml

Expand All @@ -170,7 +170,7 @@ Version
^^^^^^^

The field with the ``version`` attribute 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"``.
This is only compatible with ``int`` and ``date`` field types.

.. code-block:: xml

Expand Down
4 changes: 2 additions & 2 deletions docs/en/tutorials/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ You can provide your mapping information in Annotations or XML:
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">
<field fieldName="id" id="true" />
<id />
<field fieldName="name" type="string" />
<field fieldName="email" type="string" />
<reference-many fieldName="posts" targetDocument="Documents\BlogPost">
Expand All @@ -131,7 +131,7 @@ You can provide your mapping information in Annotations or XML:
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="Documents\BlogPost">
<field fieldName="id" id="true" />
<id />
<field fieldName="title" type="string" />
<field fieldName="body" type="string" />
<field fieldName="createdAt" type="date" />
Expand Down
14 changes: 10 additions & 4 deletions doctrine-mongo-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<xs:complexType name="document">
<xs:sequence>
<xs:element name="id" type="odm:id" minOccurs="0" maxOccurs="1"/>
<xs:element name="field" type="odm:field" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="embed-one" type="odm:embed-one" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="embed-many" type="odm:embed-many" minOccurs="0" maxOccurs="unbounded"/>
Expand Down Expand Up @@ -72,10 +73,6 @@
</xs:complexType>

<xs:complexType name="field">
<xs:sequence>
<xs:element name="id-generator-option" type="odm:id-generator-option" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="id" type="xs:boolean" default="false" />
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:attribute name="type" type="xs:NMTOKEN" />
<xs:attribute name="strategy" type="xs:NMTOKEN" default="set" />
Expand All @@ -97,6 +94,15 @@
<xs:attribute name="unique" type="xs:boolean" />
</xs:complexType>

<xs:complexType name="id">
<xs:sequence>
<xs:element name="generator-option" type="odm:id-generator-option" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="type" type="xs:NMTOKEN" />
<xs:attribute name="strategy" type="xs:NMTOKEN" default="auto" />
<xs:attribute name="fieldName" type="xs:NMTOKEN" default="id" />
</xs:complexType>

<xs:complexType name="id-generator-option">
<xs:attribute name="name" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="value" type="xs:string" use="required"/>
Expand Down
43 changes: 30 additions & 13 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,36 @@ public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Ma
if (isset($xmlRoot->{'read-preference'})) {
$class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'}));
}

if (isset($xmlRoot->id)) {
$field = $xmlRoot->id;
$mapping = [
'id' => true,
'fieldName' => 'id',
];

$attributes = $field->attributes();
foreach ($attributes as $key => $value) {
$mapping[$key] = (string) $value;
}

if (isset($mapping['strategy'])) {
$mapping['options'] = [];
if (isset($field->{'generator-option'})) {
foreach ($field->{'generator-option'} as $generatorOptions) {
$attributesGenerator = iterator_to_array($generatorOptions->attributes());
if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) {
continue;
}

$mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
}
}
}

$this->addFieldMapping($class, $mapping);
}

if (isset($xmlRoot->field)) {
foreach ($xmlRoot->field as $field) {
$mapping = [];
Expand All @@ -137,19 +167,6 @@ public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Ma

$mapping[$key] = ($mapping[$key] === 'true');
}
if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
$mapping['options'] = [];
if (isset($field->{'id-generator-option'})) {
foreach ($field->{'id-generator-option'} as $generatorOptions) {
$attributesGenerator = iterator_to_array($generatorOptions->attributes());
if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) {
continue;
}

$mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
}
}
}

if (isset($attributes['not-saved'])) {
$mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

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

<field name="id" type="string" id="true" strategy="NONE" />
<id type="string" strategy="NONE" />

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="TestDocuments\InvalidPartialFilterDocument" db="documents" collection="partialFilterDocument">
<field name="id" id="true" />
<id />
<indexes>
<index>
<key name="fieldA" order="asc" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="TestDocuments\PartialFilterDocument" db="documents" collection="partialFilterDocument">
<field name="id" id="true" />
<id />
<indexes>
<index>
<key name="fieldA" order="asc" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="TestDocuments\PrimedCollectionDocument" collection="primed_collection">
<field name="id" id="true" />
<id />

<reference-many target-document="TestDocuments\PrimedCollectionDocument" field="references" />
<reference-many target-document="TestDocuments\PrimedCollectionDocument" field="inverseMappedBy" mapped-by="references">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="TestDocuments\User" db="documents" collection="users">
<field name="id" id="true" />
<id />
<field name="username" type="string" unique="true" sparse="true"/>
<field name="createdAt" type="date" />
<field name="tags" type="collection" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="TestDocuments\UserCustomIdGenerator" db="documents" collection="users">
<field name="id" id="true" strategy="custom">
<id-generator-option name="class" value="TestDocuments\CustomIdGenerator"/>
<id-generator-option name="someOption" value="some-option"/>
</field>
<id strategy="custom">
<generator-option name="class" value="TestDocuments\CustomIdGenerator"/>
<generator-option name="someOption" value="some-option"/>
</id>
</document>
</doctrine-mongo-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="TestDocuments\UserNonStringOptions">
<field name="id" id="true" />
<id />
<reference-one target-document="Documents\Profile" field="profile" store-as="id" orphan-removal="true" />
<reference-many target-document="Documents\Group" field="groups" orphan-removal="" limit="0" skip="2" />
</document>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</tag-set>
<tag-set />
</read-preference>
<field fieldName="id" id="true" />
<id fieldName="id" />
<field fieldName="version" version="true" type="int" />
<field fieldName="lock" lock="true" type="int" />
<field fieldName="name" name="username" type="string" />
Expand Down