From 2af6e4db38bc695f93947f340925ebcfb1246751 Mon Sep 17 00:00:00 2001 From: BoShurik Date: Thu, 4 May 2023 15:36:29 +0300 Subject: [PATCH] Take into account join columns specifications. Currently, the AttributeDriver ignores any join column attribute specified on a many to many relationship. Let's copy code from the AnnotationDriver to fix that. Fixes #9902 --- .../ORM/Mapping/Driver/AttributeDriver.php | 8 ++++ .../Tests/ORM/Mapping/AttributeDriverTest.php | 39 +++++++++++++++++++ .../AttributeEntityWithNestedJoinColumns.php | 27 +++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Mapping/Fixtures/AttributeEntityWithNestedJoinColumns.php diff --git a/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php index 25c77ad3faf..525f3186111 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php @@ -433,6 +433,14 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad if ($joinTableAttribute->options) { $joinTable['options'] = $joinTableAttribute->options; } + + foreach ($joinTableAttribute->joinColumns as $joinColumn) { + $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumn); + } + + foreach ($joinTableAttribute->inverseJoinColumns as $joinColumn) { + $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumn); + } } foreach ($this->reader->getPropertyAttributeCollection($property, Mapping\JoinColumn::class) as $joinColumn) { diff --git a/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php index 3aca7b4ba42..c9ea55ddb7a 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AttributeDriverTest.php @@ -11,6 +11,7 @@ use Doctrine\ORM\Mapping\MappingAttribute; use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as PersistenceAnnotationDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriver; +use Doctrine\Tests\ORM\Mapping\Fixtures\AttributeEntityWithNestedJoinColumns; use stdClass; use function class_exists; @@ -124,6 +125,44 @@ public function testLegacyInheritance(): void self::assertTrue(is_subclass_of(AttributeDriver::class, PersistenceAnnotationDriver::class)); } + + /** + * @requires PHP 8.1 + */ + public function testManyToManyAssociationWithNestedJoinColumns(): void + { + $factory = $this->createClassMetadataFactory(); + + $metadata = $factory->getMetadataFor(AttributeEntityWithNestedJoinColumns::class); + + self::assertEquals( + [ + [ + 'name' => 'assoz_id', + 'referencedColumnName' => 'assoz_id', + 'unique' => false, + 'nullable' => true, + 'onDelete' => null, + 'columnDefinition' => null, + ], + ], + $metadata->associationMappings['assoc']['joinTable']['joinColumns'] + ); + + self::assertEquals( + [ + [ + 'name' => 'inverse_assoz_id', + 'referencedColumnName' => 'inverse_assoz_id', + 'unique' => false, + 'nullable' => true, + 'onDelete' => null, + 'columnDefinition' => null, + ], + ], + $metadata->associationMappings['assoc']['joinTable']['inverseJoinColumns'] + ); + } } #[ORM\Entity] diff --git a/tests/Doctrine/Tests/ORM/Mapping/Fixtures/AttributeEntityWithNestedJoinColumns.php b/tests/Doctrine/Tests/ORM/Mapping/Fixtures/AttributeEntityWithNestedJoinColumns.php new file mode 100644 index 00000000000..bcfb9255d70 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/Fixtures/AttributeEntityWithNestedJoinColumns.php @@ -0,0 +1,27 @@ + */ + #[ORM\ManyToMany(targetEntity: self::class)] + #[ORM\JoinTable( + name: 'assoc_table', + joinColumns: new ORM\JoinColumn(name: 'assoz_id', referencedColumnName: 'assoz_id'), + inverseJoinColumns: new ORM\JoinColumn(name: 'inverse_assoz_id', referencedColumnName: 'inverse_assoz_id') + )] + public $assoc; +}