diff --git a/src/Mapping/ManyToManyOwningSideMapping.php b/src/Mapping/ManyToManyOwningSideMapping.php index 3031e749c32..b4ca899a8dc 100644 --- a/src/Mapping/ManyToManyOwningSideMapping.php +++ b/src/Mapping/ManyToManyOwningSideMapping.php @@ -64,6 +64,7 @@ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, Na if (empty($joinColumn['referencedColumnName'])) { $mappingArray['joinTable']['joinColumns'][$key]['referencedColumnName'] = $namingStrategy->referenceColumnName(); } + if (empty($joinColumn['name'])) { $mappingArray['joinTable']['joinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName( $mappingArray['sourceEntity'], @@ -78,6 +79,7 @@ public static function fromMappingArrayAndNamingStrategy(array $mappingArray, Na if (empty($joinColumn['referencedColumnName'])) { $mappingArray['joinTable']['inverseJoinColumns'][$key]['referencedColumnName'] = $namingStrategy->referenceColumnName(); } + if (empty($joinColumn['name'])) { $mappingArray['joinTable']['inverseJoinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName( $mappingArray['targetEntity'], diff --git a/src/Mapping/ToOneOwningSideMapping.php b/src/Mapping/ToOneOwningSideMapping.php index 57ed66e6a97..718679f688d 100644 --- a/src/Mapping/ToOneOwningSideMapping.php +++ b/src/Mapping/ToOneOwningSideMapping.php @@ -107,6 +107,7 @@ public static function fromMappingArrayAndName( if (empty($joinColumn['name'])) { $mappingArray['joinColumns'][$index]['name'] = $namingStrategy->joinColumnName($mappingArray['fieldName'], $name); } + if (empty($joinColumn['referencedColumnName'])) { $mappingArray['joinColumns'][$index]['referencedColumnName'] = $namingStrategy->referenceColumnName(); } diff --git a/tests/Tests/ORM/Mapping/MappingDriverTestCase.php b/tests/Tests/ORM/Mapping/MappingDriverTestCase.php index 1f91a02de89..33b409f7f57 100644 --- a/tests/Tests/ORM/Mapping/MappingDriverTestCase.php +++ b/tests/Tests/ORM/Mapping/MappingDriverTestCase.php @@ -950,7 +950,7 @@ public function testEnumType(): void public function testCustomNamingStrategyIsRespected(): void { - $ns = new CustomPascalNamingStrategy(); + $ns = new CustomPascalNamingStrategy(); $metadata = $this->createClassMetadata(BlogPostComment::class, $ns); self::assertEquals('id', $metadata->fieldNames['Id']); @@ -1565,17 +1565,22 @@ class GH10288EnumTypeBoss extends GH10288EnumTypePerson #[Entity] class BlogPost { - #[Id, Column, GeneratedValue(strategy: 'NONE')] + #[Id] + #[Column] + #[GeneratedValue(strategy: 'NONE')] public int $id; } #[Entity] class BlogPostComment { - #[Id, Column, GeneratedValue(strategy: 'AUTO')] + #[Id] + #[Column] + #[GeneratedValue(strategy: 'AUTO')] public int $id; - #[ORM\ManyToOne, ORM\JoinColumn(nullable: false)] + #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: false)] public BlogPost $blogPost; public static function loadMetadata(ClassMetadata $metadata): void @@ -1595,12 +1600,9 @@ public static function loadMetadata(ClassMetadata $metadata): void 'targetEntity' => BlogPost::class, 'joinColumns' => [ - 0 => - [ - 'nullable' => false, - ], + 0 => ['nullable' => false], ], - ] + ], ); } } diff --git a/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php b/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php index 0936b45cd01..bb7abd47b21 100644 --- a/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php +++ b/tests/Tests/ORM/Mapping/NamingStrategy/CustomPascalNamingStrategy.php @@ -5,6 +5,14 @@ namespace Doctrine\Tests\ORM\Mapping\NamingStrategy; use Doctrine\ORM\Mapping\NamingStrategy; +use LogicException; + +use function sprintf; +use function str_contains; +use function strrpos; +use function strtolower; +use function substr; +use function ucfirst; /** * Fully customized naming strategy changing all namings to a PascalCase model. Included to test some behaviours @@ -16,6 +24,7 @@ class CustomPascalNamingStrategy implements NamingStrategy * Returns a table name for an entity class. * * @param string $className The fully-qualified class name + * * @return string A table name */ public function classToTableName(string $className): string @@ -30,14 +39,14 @@ public function classToTableName(string $className): string /** * Returns a column name for a property. * - * @param string $propertyName A property name + * @param string $propertyName A property name * @param string|null $className The fully-qualified class name * * @return string A column name */ - public function propertyToColumnName(string $propertyName, ?string $className = null): string + public function propertyToColumnName(string $propertyName, string|null $className = null): string { - if (null !== $className && strtolower($propertyName) == strtolower($this->classToTableName($className)) . 'id') { + if ($className !== null && strtolower($propertyName) === strtolower($this->classToTableName($className)) . 'id') { return 'Id'; } @@ -47,9 +56,9 @@ public function propertyToColumnName(string $propertyName, ?string $className = /** * Returns a column name for an embedded property. */ - public function embeddedFieldToColumnName(string $propertyName, string $embeddedColumnName, ?string $className = null, $embeddedClassName = null): string + public function embeddedFieldToColumnName(string $propertyName, string $embeddedColumnName, string|null $className = null, $embeddedClassName = null): string { - throw new \LogicException(sprintf('Method %s is not implemented', __METHOD__)); + throw new LogicException(sprintf('Method %s is not implemented', __METHOD__)); } /** @@ -75,13 +84,13 @@ public function joinColumnName(string $propertyName, string $className): string /** * Returns a join table name. * - * @param string $sourceEntity The source entity - * @param string $targetEntity The target entity + * @param string $sourceEntity The source entity + * @param string $targetEntity The target entity * @param string|null $propertyName A property name * * @return string A join table name */ - public function joinTableName(string $sourceEntity, string $targetEntity, ?string $propertyName = null): string + public function joinTableName(string $sourceEntity, string $targetEntity, string|null $propertyName = null): string { return $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity); } @@ -94,7 +103,7 @@ public function joinTableName(string $sourceEntity, string $targetEntity, ?strin * * @return string A join column name */ - public function joinKeyColumnName(string $entityName, ?string $referencedColumnName = null): string + public function joinKeyColumnName(string $entityName, string|null $referencedColumnName = null): string { return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName()); }