Skip to content

fix: missing unique for self-referencing relations #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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