Skip to content

Commit 3810a0b

Browse files
authored
Merge pull request #10470 from mpdude/prevent-entity-override
Deprecate overriding fields/associations inherited from other entities
2 parents db0b9d1 + c9b644d commit 3810a0b

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-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

+10-3
Original file line numberDiff line numberDiff line change
@@ -2591,10 +2591,17 @@ public function setAttributeOverride($fieldName, array $overrideMapping)
25912591

25922592
$mapping = $this->fieldMappings[$fieldName];
25932593

2594-
//if (isset($mapping['inherited'])) {
2595-
// TODO: Enable this exception in 2.8
2594+
if (isset($mapping['inherited'])) {
2595+
Deprecation::trigger(
2596+
'doctrine/orm',
2597+
'https://github.com/doctrine/orm/pull/10470',
2598+
'Overrides are only allowed for fields or associations declared in mapped superclasses or traits. This is not the case for %s::%s, which was inherited from %s. This is a misconfiguration and will be an error in Doctrine ORM 3.0.',
2599+
$this->name,
2600+
$fieldName,
2601+
$mapping['inherited']
2602+
);
25962603
//throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName);
2597-
//}
2604+
}
25982605

25992606
if (isset($mapping['id'])) {
26002607
$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

+11
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,16 @@ 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+
$cm = $this->cmf->getMetadataFor(CompanyFixContract::class);
259+
260+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/10470');
261+
262+
$cm->setAttributeOverride('completed', ['name' => 'other_column_name']);
263+
}
253264
}
254265

255266
class TransientBaseClass

0 commit comments

Comments
 (0)