From a5c008253eb325461a273ad3273b4cd1ce9859cf Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Thu, 6 Oct 2022 16:50:27 +0200 Subject: [PATCH] fix: missing unique for self-referencing relations (#395) --- CHANGELOG.md | 4 ++++ src/AttributeGenerator/DoctrineOrmAttributeGenerator.php | 4 ++-- .../AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php | 2 +- tests/e2e/customized/App/Schema/Entity/Person.php | 2 +- tests/e2e/original/App/Schema/Entity/Person.php | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2112f4c8..cb8ad65b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 5.1.1 + +* fix: missing unique for self-referencing relations + ## 5.1.0 * feat: repeatable attributes support diff --git a/src/AttributeGenerator/DoctrineOrmAttributeGenerator.php b/src/AttributeGenerator/DoctrineOrmAttributeGenerator.php index 989869d9..e418c043 100644 --- a/src/AttributeGenerator/DoctrineOrmAttributeGenerator.php +++ b/src/AttributeGenerator/DoctrineOrmAttributeGenerator.php @@ -213,7 +213,7 @@ public function generatePropertyAttributes(Property $property, string $className $attributes[] = new Attribute('ORM\JoinTable', ['name' => $relationTableName]); // Self-referencing relation if ($className === $property->reference->name()) { - $attributes[] = new Attribute('ORM\InverseJoinColumn', ['name' => $this->generateIdentifierName($this->inflector->singularize($property->name())[0].ucfirst($property->reference->name()).'Id', 'inverse_join_column', $this->config)]); + $attributes[] = new Attribute('ORM\InverseJoinColumn', ['name' => $this->generateIdentifierName($this->inflector->singularize($property->name())[0].ucfirst($property->reference->name()).'Id', 'inverse_join_column', $this->config), 'unique' => true]); } else { $attributes[] = new Attribute('ORM\InverseJoinColumn', ['unique' => true]); } @@ -227,7 +227,7 @@ public function generatePropertyAttributes(Property $property, string $className $attributes[] = new Attribute('ORM\JoinTable', ['name' => $relationTableName]); // Self-referencing relation if ($className === $property->reference->name()) { - $attributes[] = new Attribute('ORM\InverseJoinColumn', ['name' => $this->generateIdentifierName($this->inflector->singularize($property->name())[0].ucfirst($property->reference->name()).'Id', 'inverse_join_column', $this->config), 'nullable' => false]); + $attributes[] = new Attribute('ORM\InverseJoinColumn', ['name' => $this->generateIdentifierName($this->inflector->singularize($property->name())[0].ucfirst($property->reference->name()).'Id', 'inverse_join_column', $this->config), 'nullable' => false, 'unique' => true]); } else { $attributes[] = new Attribute('ORM\InverseJoinColumn', ['nullable' => false, 'unique' => true]); } diff --git a/tests/AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php b/tests/AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php index 6f635c5c..f0a86ab9 100644 --- a/tests/AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php +++ b/tests/AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php @@ -238,7 +238,7 @@ public function testGeneratePropertyAttributes(): void $this->generator->generatePropertyAttributes($this->classMap['Vehicle']->getPropertyByName('relation1_N'), 'Vehicle') ); $this->assertEquals( - [new Attribute('ORM\ManyToMany', ['targetEntity' => 'App\Entity\Vehicle']), new Attribute('ORM\JoinTable', ['name' => 'vehicle_vehicle_relation1_n_self_referencing']), new Attribute('ORM\InverseJoinColumn', ['name' => 'relation1_n_self_referencing_vehicle_id', 'nullable' => false])], + [new Attribute('ORM\ManyToMany', ['targetEntity' => 'App\Entity\Vehicle']), new Attribute('ORM\JoinTable', ['name' => 'vehicle_vehicle_relation1_n_self_referencing']), new Attribute('ORM\InverseJoinColumn', ['name' => 'relation1_n_self_referencing_vehicle_id', 'nullable' => false, 'unique' => true])], $this->generator->generatePropertyAttributes($this->classMap['Vehicle']->getPropertyByName('relation1_N_self_referencing'), 'Vehicle') ); $this->assertEquals( diff --git a/tests/e2e/customized/App/Schema/Entity/Person.php b/tests/e2e/customized/App/Schema/Entity/Person.php index 1a9197b6..f3de5863 100644 --- a/tests/e2e/customized/App/Schema/Entity/Person.php +++ b/tests/e2e/customized/App/Schema/Entity/Person.php @@ -139,7 +139,7 @@ class Person extends MyCustomClass implements MyCustomInterface */ #[ORM\ManyToMany(targetEntity: 'App\Schema\Entity\Person')] #[ORM\JoinTable(name: 'person_person_siblings')] - #[ORM\InverseJoinColumn(name: 'sibling_person_id')] + #[ORM\InverseJoinColumn(name: 'sibling_person_id', unique: true)] #[ApiProperty(types: ['https://schema.org/siblings'])] private ?Collection $siblings = null; diff --git a/tests/e2e/original/App/Schema/Entity/Person.php b/tests/e2e/original/App/Schema/Entity/Person.php index 4d7d78f2..1c5c4682 100644 --- a/tests/e2e/original/App/Schema/Entity/Person.php +++ b/tests/e2e/original/App/Schema/Entity/Person.php @@ -130,7 +130,7 @@ class Person extends Thing */ #[ORM\ManyToMany(targetEntity: 'App\Schema\Entity\Person')] #[ORM\JoinTable(name: 'person_person_siblings')] - #[ORM\InverseJoinColumn(name: 'sibling_person_id')] + #[ORM\InverseJoinColumn(name: 'sibling_person_id', unique: true)] #[ApiProperty(types: ['https://schema.org/siblings'])] private ?Collection $siblings = null;