diff --git a/UPGRADE.md b/UPGRADE.md index a0a360d72d7..5689b307bda 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,16 @@ # Upgrade to 2.19 +## Deprecate calling `ClassMetadata::getAssociationMappedByTargetField()` with the owning side of an association + +Calling +`Doctrine\ORM\Mapping\ClassMetadata::getAssociationMappedByTargetField()` with +the owning side of an association returns `null`, which is undocumented, and +wrong according to the phpdoc of the parent method. + +If you do not know whether you are on the owning or inverse side of an association, +you can use `Doctrine\ORM\Mapping\ClassMetadata::isAssociationInverseSide()` +to find out. + ## Deprecate `Doctrine\ORM\Query\Lexer::T_*` constants Use `Doctrine\ORM\Query\TokenType::T_*` instead. diff --git a/src/Mapping/ClassMetadataInfo.php b/src/Mapping/ClassMetadataInfo.php index 3e373a0956d..3780a5d44af 100644 --- a/src/Mapping/ClassMetadataInfo.php +++ b/src/Mapping/ClassMetadataInfo.php @@ -3681,6 +3681,17 @@ public function isAssociationInverseSide($fieldName) */ public function getAssociationMappedByTargetField($fieldName) { + if (! $this->isAssociationInverseSide($fieldName)) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/pull/11309', + 'Calling %s with owning side field %s is deprecated and will no longer be supported in Doctrine ORM 3.0. Call %s::isAssociationInverseSide() to check first.', + __METHOD__, + $fieldName, + self::class + ); + } + return $this->associationMappings[$fieldName]['mappedBy']; } diff --git a/tests/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Tests/ORM/Mapping/ClassMetadataTest.php index d1e82c1922b..48079777276 100644 --- a/tests/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Tests/ORM/Mapping/ClassMetadataTest.php @@ -6,6 +6,7 @@ use ArrayObject; use Doctrine\DBAL\Types\Types; +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use Doctrine\ORM\Events; use Doctrine\ORM\Mapping\ChainTypedFieldMapper; use Doctrine\ORM\Mapping\ClassMetadata; @@ -54,6 +55,8 @@ class ClassMetadataTest extends OrmTestCase { + use VerifyDeprecations; + public function testClassMetadataInstanceSerialization(): void { $cm = new ClassMetadata(CMS\CmsUser::class); @@ -1379,6 +1382,16 @@ public function testRejectsEmbeddableWithoutValidClassName(): void 'columnPrefix' => false, ]); } + + public function testInvalidCallToGetAssociationMappedByTargetFieldIsDeprecated(): void + { + $metadata = new ClassMetadata(self::class); + $metadata->mapOneToOne(['fieldName' => 'foo', 'targetEntity' => 'bar']); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/11309'); + + $metadata->getAssociationMappedByTargetField('foo'); + } } /** @MappedSuperclass */