Skip to content

Commit

Permalink
Trigger a deprecation notice when entity fields/associations are over…
Browse files Browse the repository at this point in the history
…ridden

This was brought up in doctrine#8348, but seemingly forgotten to be implenented in later versions.

Closes doctrine#10289.
  • Loading branch information
mpdude committed Feb 9, 2023
1 parent 2ee936a commit c9b644d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Upgrade to 2.15

## Deprecated overriding fields or associations not declared in mapped superclasses

As stated in the documentation, fields and associations may only be overridden when being inherited
from mapped superclasses. Overriding them for parent entity classes now triggers a deprecation notice
and will be an error in 3.0.

## Deprecated undeclared entity inheritance

As soon as an entity class inherits from another entity class, inheritance has to
Expand Down
13 changes: 10 additions & 3 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2693,10 +2693,17 @@ public function setAttributeOverride($fieldName, array $overrideMapping)

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

//if (isset($mapping['inherited'])) {
// TODO: Enable this exception in 2.8
if (isset($mapping['inherited'])) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/10470',
'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.',
$this->name,
$fieldName,
$mapping['inherited']
);
//throw MappingException::illegalOverrideOfInheritedProperty($this->name, $fieldName);
//}
}

if (isset($mapping['id'])) {
$overrideMapping['id'] = $mapping['id'];
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,7 @@ public static function illegalOverrideOfInheritedProperty($className, $propertyN
{
return new self(
sprintf(
'Override for %s::%s is only allowed for attributes/associations ' .
'declared on a mapped superclass or a trait.',
'Overrides are only allowed for fields or associations declared in mapped superclasses or traits, which is not the case for %s::%s.',
$className,
$propertyName
)
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
use Doctrine\Tests\Models\Company\CompanyFixContract;
use Doctrine\Tests\Models\DDC869\DDC869ChequePayment;
use Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment;
use Doctrine\Tests\Models\DDC869\DDC869Payment;
Expand Down Expand Up @@ -250,6 +251,16 @@ public function invalidHierarchyDeclarationClasses(): Generator
yield 'complex example (Entity Root -> Mapped Superclass -> transient class -> Entity)'
=> [InvalidComplexRoot::class, InvalidComplexEntity::class];
}

/** @group DDC-964 */
public function testInvalidOverrideFieldInheritedFromEntity(): void
{
$cm = $this->cmf->getMetadataFor(CompanyFixContract::class);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/10470');

$cm->setAttributeOverride('completed', ['name' => 'other_column_name']);
}
}

class TransientBaseClass
Expand Down

0 comments on commit c9b644d

Please sign in to comment.