From fac8d84fdb371ae849246e2743885886606c9f6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Tue, 2 May 2023 23:36:49 +0200 Subject: [PATCH] Throw on invalid one to one One to one relationships are not allowed to define a join column. That should be done on the owning side of the relationship. --- lib/Doctrine/ORM/Mapping/ClassMetadata.php | 11 +++-------- lib/Doctrine/ORM/Mapping/MappingException.php | 9 +++++++++ psalm-baseline.xml | 12 +----------- .../Tests/ORM/Mapping/ClassMetadataTest.php | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadata.php b/lib/Doctrine/ORM/Mapping/ClassMetadata.php index 4e05f48ba55..5a0c5e401c8 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadata.php @@ -1373,17 +1373,12 @@ protected function _validateAndCompleteAssociationMapping(array $mapping): Assoc switch ($mapping['type']) { case self::ONE_TO_ONE: if (isset($mapping['joinColumns']) && $mapping['joinColumns'] && ! $mapping['isOwningSide']) { - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/orm/pull/10654', - 'JoinColumn configuration is not allowed on the inverse side of one-to-one associations, and will throw a MappingException in Doctrine ORM 3.0', + throw MappingException::joinColumnNotAllowedOnOneToOneInverseSide( + $this->name, + $mapping['fieldName'], ); } - if (isset($mapping['joinColumns']) && $mapping['joinColumns']) { - $mapping['isOwningSide'] = true; - } - return $mapping['isOwningSide'] ? OneToOneOwningSideMapping::fromMappingArrayAndName( $mapping, diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index b172c174c26..6dddeb0dc1d 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -230,6 +230,15 @@ public static function joinColumnMustPointToMappedField(string $className, strin )); } + public static function joinColumnNotAllowedOnOneToOneInverseSide(string $className, string $fieldName): self + { + return new self(sprintf( + '%s#%s is a OneToOne inverse side, which does not allow join columns.', + $className, + $fieldName, + )); + } + /** @param class-string $className */ public static function classIsNotAValidEntityOrMappedSuperClass(string $className): self { diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1ac354fb1c7..3c8268feb7c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -347,6 +347,7 @@ setValue + @@ -359,7 +360,6 @@ $mapping !== false $mapping !== false - array_values @@ -368,15 +368,6 @@ table]]> null - - namingStrategy, - $this->name, - $this->table, - $this->isInheritanceTypeSingleTable(), - )]]> - @@ -730,7 +721,6 @@ array ]]> - getValue getValue diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index d75ca619570..7b50ec54c7f 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -331,6 +331,23 @@ public function testSetInvalidVersionMappingThrowsException(): void $cm->setVersionMapping($field); } + public function testItThrowsOnJoinColumnForInverseOneToOne(): void + { + $cm = new ClassMetadata(CmsUser::class); + $cm->initializeReflection(new RuntimeReflectionService()); + + $this->expectException(MappingException::class); + $this->expectExceptionMessage( + 'Doctrine\Tests\Models\CMS\CmsUser#address is a OneToOne inverse side, which does not allow join columns.', + ); + $cm->mapOneToOne([ + 'fieldName' => 'address', + 'targetEntity' => CmsAddress::class, + 'mappedBy' => 'user', + 'joinColumns' => ['non', 'empty', 'list'], + ]); + } + public function testGetSingleIdentifierFieldNameMultipleIdentifierEntityThrowsException(): void { $cm = new ClassMetadata(CmsUser::class);