diff --git a/lib/Doctrine/ORM/Mapping/JoinColumnMapping.php b/lib/Doctrine/ORM/Mapping/JoinColumnMapping.php index 37ea4728b61..bc469bb6494 100644 --- a/lib/Doctrine/ORM/Mapping/JoinColumnMapping.php +++ b/lib/Doctrine/ORM/Mapping/JoinColumnMapping.php @@ -13,7 +13,6 @@ final class JoinColumnMapping implements ArrayAccess { use ArrayAccessImplementation; - public string|null $name = null; public bool|null $unique = null; public bool|null $quoted = null; public string|null $fieldName = null; @@ -25,6 +24,7 @@ final class JoinColumnMapping implements ArrayAccess public array|null $options = null; public function __construct( + public string $name, public string $referencedColumnName, ) { } @@ -35,7 +35,7 @@ public function __construct( */ public static function fromMappingArray(array $mappingArray): self { - $mapping = new self($mappingArray['referencedColumnName']); + $mapping = new self($mappingArray['name'], $mappingArray['referencedColumnName']); foreach ($mappingArray as $key => $value) { if (property_exists($mapping, $key) && $value !== null) { $mapping->$key = $value; diff --git a/lib/Doctrine/ORM/Mapping/ManyToManyOwningSideMapping.php b/lib/Doctrine/ORM/Mapping/ManyToManyOwningSideMapping.php index 4a4f3e0112e..c7a0eb4ad58 100644 --- a/lib/Doctrine/ORM/Mapping/ManyToManyOwningSideMapping.php +++ b/lib/Doctrine/ORM/Mapping/ManyToManyOwningSideMapping.php @@ -47,8 +47,30 @@ public function toArray(): array */ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, NamingStrategy $namingStrategy): self { + if (isset($mappingArray['joinTable']['joinColumns'])) { + foreach ($mappingArray['joinTable']['joinColumns'] as $key => $joinColumn) { + if (empty($joinColumn['name'])) { + $mappingArray['joinTable']['joinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName( + $mappingArray['sourceEntity'], + $joinColumn['referencedColumnName'] ?? null, + ); + } + } + } + + if (isset($mappingArray['joinTable']['inverseJoinColumns'])) { + foreach ($mappingArray['joinTable']['inverseJoinColumns'] as $key => $joinColumn) { + if (empty($joinColumn['name'])) { + $mappingArray['joinTable']['inverseJoinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName( + $mappingArray['targetEntity'], + $joinColumn['referencedColumnName'] ?? null, + ); + } + } + } + // owning side MUST have a join table - if (! isset($mappingArray['joinTable']['name'])) { + if (! isset($mappingArray['joinTable']) || ! isset($mappingArray['joinTable']['name'])) { $mappingArray['joinTable']['name'] = $namingStrategy->joinTableName( $mappingArray['sourceEntity'], $mappingArray['targetEntity'], @@ -85,10 +107,6 @@ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, Na $mapping->joinTableColumns = []; foreach ($mapping->joinTable->joinColumns as $joinColumn) { - if (empty($joinColumn->name)) { - $joinColumn->name = $namingStrategy->joinKeyColumnName($mapping->sourceEntity, $joinColumn->referencedColumnName); - } - if (empty($joinColumn->referencedColumnName)) { $joinColumn->referencedColumnName = $namingStrategy->referenceColumnName(); } @@ -112,10 +130,6 @@ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, Na } foreach ($mapping->joinTable->inverseJoinColumns as $inverseJoinColumn) { - if (empty($inverseJoinColumn->name)) { - $inverseJoinColumn->name = $namingStrategy->joinKeyColumnName($mapping->targetEntity, $inverseJoinColumn->referencedColumnName); - } - if (empty($inverseJoinColumn->referencedColumnName)) { $inverseJoinColumn->referencedColumnName = $namingStrategy->referenceColumnName(); } diff --git a/lib/Doctrine/ORM/Mapping/ToOneOwningSideMapping.php b/lib/Doctrine/ORM/Mapping/ToOneOwningSideMapping.php index b77e8e61ee5..d28074bb03e 100644 --- a/lib/Doctrine/ORM/Mapping/ToOneOwningSideMapping.php +++ b/lib/Doctrine/ORM/Mapping/ToOneOwningSideMapping.php @@ -74,6 +74,14 @@ public static function fromMappingArrayAndName( array|null $table, bool $isInheritanceTypeSingleTable, ): static { + if (isset($mappingArray['joinColumns'])) { + foreach ($mappingArray['joinColumns'] as $index => $joinColumn) { + if (empty($joinColumn['name'])) { + $mappingArray['joinColumns'][$index]['name'] = $namingStrategy->joinColumnName($mappingArray['fieldName'], $name); + } + } + } + $mapping = static::fromMappingArray($mappingArray); assert($mapping->isToOneOwningSide()); @@ -100,10 +108,6 @@ public static function fromMappingArrayAndName( } } - if (empty($joinColumn->name)) { - $joinColumn->name = $namingStrategy->joinColumnName($mapping->fieldName, $name); - } - if (empty($joinColumn->referencedColumnName)) { $joinColumn->referencedColumnName = $namingStrategy->referenceColumnName(); } diff --git a/tests/Doctrine/Tests/ORM/Mapping/JoinColumnMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/JoinColumnMappingTest.php index e79d1b44be5..b5459db7f76 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/JoinColumnMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/JoinColumnMappingTest.php @@ -15,9 +15,8 @@ final class JoinColumnMappingTest extends TestCase { public function testItSurvivesSerialization(): void { - $mapping = new JoinColumnMapping('id'); + $mapping = new JoinColumnMapping('foo', 'id'); - $mapping->name = 'foo'; $mapping->unique = true; $mapping->quoted = true; $mapping->fieldName = 'bar'; diff --git a/tests/Doctrine/Tests/ORM/Mapping/JoinTableMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/JoinTableMappingTest.php index 8077c15f3e0..65c00ed0fe6 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/JoinTableMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/JoinTableMappingTest.php @@ -19,8 +19,8 @@ public function testItSurvivesSerialization(): void $mapping = new JoinTableMapping('bar'); $mapping->quoted = true; - $mapping->joinColumns = [new JoinColumnMapping('id')]; - $mapping->inverseJoinColumns = [new JoinColumnMapping('id')]; + $mapping->joinColumns = [new JoinColumnMapping('foo_id', 'id')]; + $mapping->inverseJoinColumns = [new JoinColumnMapping('bar_id', 'id')]; $mapping->schema = 'foo'; $mapping->options = ['foo' => 'bar']; diff --git a/tests/Doctrine/Tests/ORM/Mapping/ManyToOneAssociationMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/ManyToOneAssociationMappingTest.php index d183f9df158..3833e51ef53 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ManyToOneAssociationMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ManyToOneAssociationMappingTest.php @@ -22,7 +22,7 @@ public function testItSurvivesSerialization(): void targetEntity: self::class, ); - $mapping->joinColumns = [new JoinColumnMapping('id')]; + $mapping->joinColumns = [new JoinColumnMapping('foo_id', 'id')]; $mapping->joinColumnFieldNames = ['foo' => 'bar']; $mapping->sourceToTargetKeyColumns = ['foo' => 'bar']; $mapping->targetToSourceKeyColumns = ['bar' => 'foo']; diff --git a/tests/Doctrine/Tests/ORM/Mapping/OneToOneOwningSideMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/OneToOneOwningSideMappingTest.php index f8d3ea0e8c6..73875653da2 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/OneToOneOwningSideMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/OneToOneOwningSideMappingTest.php @@ -22,7 +22,7 @@ public function testItSurvivesSerialization(): void targetEntity: self::class, ); - $mapping->joinColumns = [new JoinColumnMapping('id')]; + $mapping->joinColumns = [new JoinColumnMapping('foo_id', 'id')]; $mapping->joinColumnFieldNames = ['foo' => 'bar']; $mapping->sourceToTargetKeyColumns = ['foo' => 'bar']; $mapping->targetToSourceKeyColumns = ['bar' => 'foo'];