Skip to content

Commit

Permalink
Add deprecation layer for fieldName attributes (#1834)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jan 9, 2019
1 parent 5ab33e4 commit f8a2964
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
6 changes: 6 additions & 0 deletions UPGRADE-1.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ The `Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo` class has been deprecated
in favor of `Doctrine\ODM\MongoDB\Mapping\ClassMetadata` and will be dropped in
2.0.

### XML mappings

* The `writeConcern` attribute in document mappings has been deprecated and
will be dropped in 2.0. Use `write-concern` instead.
* The `fieldName` attribute in field mappings has been deprecated and will be
dropped in 2.0. Use `field-name` instead.
12 changes: 12 additions & 0 deletions doctrine-mongo-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
</xs:choice>
<xs:attribute name="db" type="xs:NMTOKEN" />
<xs:attribute name="name" type="xs:string" />
<!-- deprecated -->
<xs:attribute name="writeConcern" type="xs:string" />
<xs:attribute name="write-concern" type="xs:string" />
<xs:attribute name="collection" type="xs:NMTOKEN" />
<xs:attribute name="capped-collection" type="xs:boolean" />
<xs:attribute name="capped-collection-size" type="xs:integer" />
Expand Down Expand Up @@ -84,7 +86,9 @@
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:attribute name="type" type="xs:NMTOKEN" />
<xs:attribute name="strategy" type="xs:NMTOKEN" default="set" />
<!-- deprecated -->
<xs:attribute name="fieldName" type="xs:NMTOKEN" />
<xs:attribute name="field-name" type="xs:NMTOKEN" />
<xs:attribute name="embed" type="xs:boolean" />
<xs:attribute name="file" type="xs:boolean" />
<xs:attribute name="distance" type="xs:boolean" />
Expand Down Expand Up @@ -127,7 +131,9 @@
</xs:choice>
<xs:attribute name="target-document" type="xs:string" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
<!-- deprecated -->
<xs:attribute name="fieldName" type="xs:NMTOKEN" />
<xs:attribute name="field-name" type="xs:NMTOKEN" />
<xs:attribute name="not-saved" type="xs:boolean" />
<xs:attribute name="nullable" type="xs:boolean" />
<xs:attribute name="also-load" type="xs:NMTOKEN" />
Expand All @@ -142,7 +148,9 @@
<xs:attribute name="target-document" type="xs:string" />
<xs:attribute name="collection-class" type="xs:string" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
<!-- deprecated -->
<xs:attribute name="fieldName" type="xs:NMTOKEN" />
<xs:attribute name="field-name" type="xs:NMTOKEN" />
<xs:attribute name="strategy" type="xs:NMTOKEN" default="pushAll" />
<xs:attribute name="not-saved" type="xs:boolean" />
<xs:attribute name="nullable" type="xs:boolean" />
Expand All @@ -169,7 +177,9 @@
</xs:choice>
<xs:attribute name="target-document" type="xs:string" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
<!-- deprecated -->
<xs:attribute name="fieldName" type="xs:NMTOKEN" />
<xs:attribute name="field-name" type="xs:NMTOKEN" />
<!-- deprecated -->
<xs:attribute name="simple" type="xs:boolean" />
<xs:attribute name="store-as" type="odm:reference-store-as" default="dbRefWithDb" />
Expand All @@ -195,7 +205,9 @@
<xs:attribute name="target-document" type="xs:string" />
<xs:attribute name="collection-class" type="xs:string" />
<xs:attribute name="field" type="xs:NMTOKEN" use="required" />
<!-- deprecated -->
<xs:attribute name="fieldName" type="xs:NMTOKEN" />
<xs:attribute name="field-name" type="xs:NMTOKEN" />
<!-- deprecated -->
<xs:attribute name="simple" type="xs:boolean" />
<xs:attribute name="store-as" type="odm:reference-store-as" default="dbRefWithDb" />
Expand Down
41 changes: 38 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ public function loadMetadataForClass($className, ClassMetadata $class)
$class->setCollection((string) $xmlRoot['collection']);
}
}
if (isset($xmlRoot['writeConcern'])) {

if (isset($xmlRoot['write-concern'])) {
$class->setWriteConcern((string) $xmlRoot['write-concern']);
} elseif (isset($xmlRoot['writeConcern'])) {
@trigger_error(sprintf('The "writeConcern" attribute in the mapping file for class "%s" is deprecated and will be dropped in 2.0. Please use "write-concern" instead.', $class->getName()), E_USER_DEPRECATED);
$class->setWriteConcern((string) $xmlRoot['writeConcern']);
}

if (isset($xmlRoot['inheritance-type'])) {
$inheritanceType = (string) $xmlRoot['inheritance-type'];
$class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType));
Expand Down Expand Up @@ -206,6 +211,19 @@ public function loadMetadataForClass($className, ClassMetadata $class)
$mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
}

if (isset($attributes['field-name'])) {
$mapping['fieldName'] = (string) $attributes['field-name'];
} elseif (isset($attributes['fieldName'])) {
@trigger_error(
sprintf(
'Field "%s" in class "%s" is mapped using a "fieldName" attribute which is deprecated. Use the "field-name" attribute instead.',
isset($mapping['name']) ? $mapping['name'] : $mapping['fieldName'],
$class->getName()
),
E_USER_DEPRECATED
);
}

if (isset($attributes['also-load'])) {
$mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
} elseif (isset($attributes['version'])) {
Expand Down Expand Up @@ -303,9 +321,18 @@ private function addEmbedMapping(MappingClassMetadata $class, $embed, $type)
'name' => (string) $attributes['field'],
'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
);
if (isset($attributes['fieldName'])) {

if (isset($attributes['field-name'])) {
$mapping['fieldName'] = (string) $attributes['field-name'];
} elseif (isset($attributes['fieldName'])) {
$mapping['fieldName'] = (string) $attributes['fieldName'];

@trigger_error(
sprintf('Field "%s" in class "%s" is mapped using a "fieldName" attribute which is deprecated. Use the "field-name" attribute instead.', $mapping['name'], $class->getName()),
E_USER_DEPRECATED
);
}

if (isset($embed->{'discriminator-field'})) {
$attr = $embed->{'discriminator-field'};
$mapping['discriminatorField'] = (string) $attr['name'];
Expand Down Expand Up @@ -355,9 +382,17 @@ private function addReferenceMapping(MappingClassMetadata $class, $reference, $t
'prime' => [],
);

if (isset($attributes['fieldName'])) {
if (isset($attributes['field-name'])) {
$mapping['fieldName'] = (string) $attributes['field-name'];
} elseif (isset($attributes['fieldName'])) {
$mapping['fieldName'] = (string) $attributes['fieldName'];

@trigger_error(
sprintf('Field "%s" in class "%s" is mapped using a "fieldName" attribute which is deprecated. Use the "field-name" attribute instead.', $mapping['name'], $class->getName()),
E_USER_DEPRECATED
);
}

if (isset($reference->{'discriminator-field'})) {
$attr = $reference->{'discriminator-field'};
$mapping['discriminatorField'] = (string) $attr['name'];
Expand Down

0 comments on commit f8a2964

Please sign in to comment.