Skip to content
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
8 changes: 4 additions & 4 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/Maker/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
[
Expand Down
53 changes: 53 additions & 0 deletions tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class Directory
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;

/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;

public function getId()
{
return $this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Tests;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManager;
use App\Entity\Directory;

class GeneratedEntityTest extends KernelTestCase
{
public function testGeneratedEntity()
{
self::bootKernel();
/** @var EntityManager $em */
$em = self::$kernel->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());
}
}