Skip to content

Commit

Permalink
fix: missing unique for self-referencing relations (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain authored Oct 6, 2022
1 parent f737d78 commit a5c0082
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 5.1.1

* fix: missing unique for self-referencing relations

## 5.1.0

* feat: repeatable attributes support
Expand Down
4 changes: 2 additions & 2 deletions src/AttributeGenerator/DoctrineOrmAttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand All @@ -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]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/customized/App/Schema/Entity/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/original/App/Schema/Entity/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit a5c0082

Please sign in to comment.