Skip to content

Commit a5c0082

Browse files
authored
fix: missing unique for self-referencing relations (#395)
1 parent f737d78 commit a5c0082

File tree

5 files changed

+9
-5
lines changed

5 files changed

+9
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 5.1.1
4+
5+
* fix: missing unique for self-referencing relations
6+
37
## 5.1.0
48

59
* feat: repeatable attributes support

src/AttributeGenerator/DoctrineOrmAttributeGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function generatePropertyAttributes(Property $property, string $className
213213
$attributes[] = new Attribute('ORM\JoinTable', ['name' => $relationTableName]);
214214
// Self-referencing relation
215215
if ($className === $property->reference->name()) {
216-
$attributes[] = new Attribute('ORM\InverseJoinColumn', ['name' => $this->generateIdentifierName($this->inflector->singularize($property->name())[0].ucfirst($property->reference->name()).'Id', 'inverse_join_column', $this->config)]);
216+
$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]);
217217
} else {
218218
$attributes[] = new Attribute('ORM\InverseJoinColumn', ['unique' => true]);
219219
}
@@ -227,7 +227,7 @@ public function generatePropertyAttributes(Property $property, string $className
227227
$attributes[] = new Attribute('ORM\JoinTable', ['name' => $relationTableName]);
228228
// Self-referencing relation
229229
if ($className === $property->reference->name()) {
230-
$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]);
230+
$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]);
231231
} else {
232232
$attributes[] = new Attribute('ORM\InverseJoinColumn', ['nullable' => false, 'unique' => true]);
233233
}

tests/AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function testGeneratePropertyAttributes(): void
238238
$this->generator->generatePropertyAttributes($this->classMap['Vehicle']->getPropertyByName('relation1_N'), 'Vehicle')
239239
);
240240
$this->assertEquals(
241-
[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])],
241+
[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])],
242242
$this->generator->generatePropertyAttributes($this->classMap['Vehicle']->getPropertyByName('relation1_N_self_referencing'), 'Vehicle')
243243
);
244244
$this->assertEquals(

tests/e2e/customized/App/Schema/Entity/Person.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Person extends MyCustomClass implements MyCustomInterface
139139
*/
140140
#[ORM\ManyToMany(targetEntity: 'App\Schema\Entity\Person')]
141141
#[ORM\JoinTable(name: 'person_person_siblings')]
142-
#[ORM\InverseJoinColumn(name: 'sibling_person_id')]
142+
#[ORM\InverseJoinColumn(name: 'sibling_person_id', unique: true)]
143143
#[ApiProperty(types: ['https://schema.org/siblings'])]
144144
private ?Collection $siblings = null;
145145

tests/e2e/original/App/Schema/Entity/Person.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Person extends Thing
130130
*/
131131
#[ORM\ManyToMany(targetEntity: 'App\Schema\Entity\Person')]
132132
#[ORM\JoinTable(name: 'person_person_siblings')]
133-
#[ORM\InverseJoinColumn(name: 'sibling_person_id')]
133+
#[ORM\InverseJoinColumn(name: 'sibling_person_id', unique: true)]
134134
#[ApiProperty(types: ['https://schema.org/siblings'])]
135135
private ?Collection $siblings = null;
136136

0 commit comments

Comments
 (0)