Skip to content

Commit a4ca839

Browse files
committed
Trigger a deprecation notice when entity fields/associations are overridden
This was brought up in #8348, but seemingly forgotten to be implenented in later versions. Closes #10289.
1 parent 2ee936a commit a4ca839

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

UPGRADE.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Upgrade to 2.15
22

3+
## Deprecated overriding fields or associations not declared in mapped superclasses
4+
5+
As stated in the documentation, fields and associations may only be overridden when being inherited
6+
from mapped superclasses. Overriding them for parent entity classes now triggers a deprecation notice
7+
and will be an error in 3.0.
8+
39
## Deprecated undeclared entity inheritance
410

511
As soon as an entity class inherits from another entity class, inheritance has to

lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -2693,10 +2693,16 @@ public function setAttributeOverride($fieldName, array $overrideMapping)
26932693

26942694
$mapping = $this->fieldMappings[$fieldName];
26952695

2696-
//if (isset($mapping['inherited'])) {
2697-
// TODO: Enable this exception in 2.8
2696+
if (isset($mapping['inherited'])) {
2697+
Deprecation::trigger(
2698+
'doctrine/orm',
2699+
'https://github.com/doctrine/orm/pull/10470',
2700+
'Overrides are only allowed for fields or associations declared in mapped superclasses or traits, which is not the case for %s::%s. This is a misconfiguration and will be an error in Doctrine ORM 3.0.',
2701+
$this->name,
2702+
$fieldName
2703+
);
26982704
//throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName);
2699-
//}
2705+
}
27002706

27012707
if (isset($mapping['id'])) {
27022708
$overrideMapping['id'] = $mapping['id'];

lib/Doctrine/ORM/Mapping/MappingException.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,7 @@ public static function illegalOverrideOfInheritedProperty($className, $propertyN
908908
{
909909
return new self(
910910
sprintf(
911-
'Override for %s::%s is only allowed for attributes/associations ' .
912-
'declared on a mapped superclass or a trait.',
911+
'Overrides are only allowed for fields or associations declared in mapped superclasses or traits, which is not the case for %s::%s.',
913912
$className,
914913
$propertyName
915914
)

tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Doctrine\ORM\Mapping\Table;
2626
use Doctrine\ORM\Mapping\UniqueConstraint;
2727
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
28+
use Doctrine\Tests\Models\Company\CompanyFixContract;
2829
use Doctrine\Tests\Models\DDC869\DDC869ChequePayment;
2930
use Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment;
3031
use Doctrine\Tests\Models\DDC869\DDC869Payment;
@@ -250,6 +251,15 @@ public function invalidHierarchyDeclarationClasses(): Generator
250251
yield 'complex example (Entity Root -> Mapped Superclass -> transient class -> Entity)'
251252
=> [InvalidComplexRoot::class, InvalidComplexEntity::class];
252253
}
254+
255+
/** @group DDC-964 */
256+
public function testInvalidOverrideFieldInheritedFromEntity(): void
257+
{
258+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/10470');
259+
260+
$cm = $this->cmf->getMetadataFor(CompanyFixContract::class);
261+
$cm->setAttributeOverride('completed', ['name' => 'other_column_name']);
262+
}
253263
}
254264

255265
class TransientBaseClass

0 commit comments

Comments
 (0)