diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index bea68cd83..c4b7b368f 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -468,16 +468,16 @@ private function askRelationDetails(ConsoleStyle $io, string $generatedEntityCla $targetEntityClass = $io->askQuestion($question); - if (!class_exists($targetEntityClass)) { - if (!class_exists($this->getEntityNamespace().'\\'.$targetEntityClass)) { + if (!class_exists($designatedTargetClass = $this->getEntityNamespace().'\\'.$targetEntityClass)) { + if (!class_exists($designatedTargetClass = $targetEntityClass)) { $io->error(sprintf('Unknown class "%s"', $targetEntityClass)); $targetEntityClass = null; continue; } - - $targetEntityClass = $this->getEntityNamespace().'\\'.$targetEntityClass; } + + $targetEntityClass = $designatedTargetClass; } // help the user select the type diff --git a/tests/Maker/FunctionalTest.php b/tests/Maker/FunctionalTest.php index 7b709353c..266cc5998 100644 --- a/tests/Maker/FunctionalTest.php +++ b/tests/Maker/FunctionalTest.php @@ -906,6 +906,36 @@ public function getCommandEntityTests() ->setRequiredPhpVersion(70100) ]; + yield 'entity_exists_in_root' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'Directory', + // field name + 'parentDirectory', + // add a relationship field + 'relation', + // the target entity + 'Directory', + // relation type + 'ManyToOne', + // nullable + 'y', + // do you want to generate an inverse relation? (default to yes) + '', + // field name on opposite side + 'childDirectories', + // orphanRemoval (default to no) + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityExistsInRoot') + ->configureDatabase() + ->updateSchemaAfterCommand() + ->setRequiredPhpVersion(70100) + ]; + yield 'entity_one_to_many_simple' => [MakerTestDetails::createTest( $this->getMakerInstance(MakeEntity::class), [ diff --git a/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php b/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php new file mode 100644 index 000000000..0ec7ee4f6 --- /dev/null +++ b/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php @@ -0,0 +1,53 @@ +id; + } + + public function getName() + { + return $this->name; + } + + public function setName(?string $name) + { + $this->name = $name; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/MakeEntityExistsInRoot/tests/GeneratedEntityTest.php b/tests/fixtures/MakeEntityExistsInRoot/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..628e2cec2 --- /dev/null +++ b/tests/fixtures/MakeEntityExistsInRoot/tests/GeneratedEntityTest.php @@ -0,0 +1,49 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\Directory u')->execute(); + + $directory = new Directory(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $directory->getChildDirectories()); + // set existing field + $directory->setName('root'); + $em->persist($directory); + + $subDir = new Directory(); + $subDir->setName('settings'); + $subDir->setParentDirectory($directory); + $em->persist($subDir); + + // set via the inverse side + $subDir2 = new Directory(); + $subDir2->setName('fixtures'); + $directory->addChildDirectory($subDir2); + $em->persist($subDir2); + + $em->flush(); + $em->refresh($directory); + + $actualDirectory = $em->getRepository(Directory::class) + ->findAll(); + + $this->assertCount(3, $actualDirectory); + $this->assertCount(2, $actualDirectory[0]->getChildDirectories()); + } +}