Skip to content

Commit 6db8126

Browse files
committed
Add optimizations for self-referencing entities
1 parent 732edbf commit 6db8126

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

src/Maker/MakeEntity.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
201201
} elseif ($newField instanceof EntityRelation) {
202202
// both overridden below for OneToMany
203203
$newFieldName = $newField->getOwningProperty();
204-
if ($newField->getInverseClass() !== $newField->getOwningClass()) {
205-
$otherManipulatorFilename = $this->getPathOfClass($newField->getInverseClass());
206-
$otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite);
207-
} else {
204+
if ($newField->isSelfReferencing()) {
208205
$otherManipulatorFilename = $entityPath;
209206
$otherManipulator = $manipulator;
207+
} else {
208+
$otherManipulatorFilename = $this->getPathOfClass($newField->getInverseClass());
209+
$otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite);
210210
}
211211
switch ($newField->getType()) {
212212
case EntityRelation::MANY_TO_ONE:

src/Util/ClassSourceManipulator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ private function addSingularRelation(BaseRelation $relation)
447447

448448
private function addCollectionRelation(BaseCollectionRelation $relation)
449449
{
450-
$typeHint = !$relation->isSelfReferencing() ? $this->addUseStatementIfNecessary($relation->getTargetClassName()) : 'self';
450+
$typeHint = $relation->isSelfReferencing() ? 'self' : $this->addUseStatementIfNecessary($relation->getTargetClassName());
451451

452452
$arrayCollectionTypeHint = $this->addUseStatementIfNecessary(ArrayCollection::class);
453453
$collectionTypeHint = $this->addUseStatementIfNecessary(Collection::class);

tests/Maker/FunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ public function getCommandEntityTests()
823823
// do you want to generate an inverse relation? (default to yes)
824824
'',
825825
// field name on opposite side
826-
'wards',
826+
'dependants',
827827
// orphanRemoval (default to no)
828828
'',
829829
// finish adding fields

tests/fixtures/MakeEntitySelfReferencing/tests/GeneratedEntityTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testGeneratedEntity()
2121

2222
$user = new User();
2323
// check that the constructor was instantiated properly
24-
$this->assertInstanceOf(ArrayCollection::class, $user->getWards());
24+
$this->assertInstanceOf(ArrayCollection::class, $user->getDependants());
2525
// set existing field
2626
$user->setFirstName('Ryan');
2727
$em->persist($user);
@@ -34,7 +34,7 @@ public function testGeneratedEntity()
3434
// set via the inverse side
3535
$ward2 = new User();
3636
$ward2->setFirstName('Fabien');
37-
$user->addWard($ward2);
37+
$user->addDependant($ward2);
3838
$em->persist($ward2);
3939

4040
$em->flush();
@@ -44,6 +44,6 @@ public function testGeneratedEntity()
4444
->findAll();
4545

4646
$this->assertCount(3, $actualUser);
47-
$this->assertCount(2, $actualUser[0]->getWards());
47+
$this->assertCount(2, $actualUser[0]->getDependants());
4848
}
4949
}

0 commit comments

Comments
 (0)