Skip to content

Commit 2ba2b56

Browse files
committed
bug #311 Fix Entity directory does not exist (yceruto)
This PR was squashed before being merged into the 1.0-dev branch (closes #311). Discussion ---------- Fix Entity directory does not exist Closes #299 according to #185 (comment) Also alternative for #185, #265 and #310 Commits ------- 5f37c5d Remove invalid field mapping 06f5399 Invalidating cached AnnotationDriver::$classNames to find new entities 8b7c291 Fix 'src/Entity' directory does not exist
2 parents 08737e6 + 5f37c5d commit 2ba2b56

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed

src/Doctrine/DoctrineHelper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MakerBundle\Doctrine;
1313

1414
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
15+
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver;
1516
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
1617
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
1718
use Doctrine\ORM\EntityManagerInterface;
@@ -145,6 +146,19 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
145146
foreach ($loaded as $m) {
146147
$cmf->setMetadataFor($m->getName(), $m);
147148
}
149+
150+
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
151+
$metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();
152+
if ($metadataDriver instanceof MappingDriverChain) {
153+
foreach ($metadataDriver->getDrivers() as $driver) {
154+
if ($driver instanceof AnnotationDriver) {
155+
$classNames = (new \ReflectionObject($driver))->getProperty('classNames');
156+
$classNames->setAccessible(true);
157+
$classNames->setValue($driver, null);
158+
$classNames->setAccessible(false);
159+
}
160+
}
161+
}
148162
}
149163

150164
foreach ($cmf->getAllMetadata() as $m) {

src/Maker/MakeEntity.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
use Symfony\Component\Console\Input\InputOption;
3737
use Symfony\Component\Console\Question\ConfirmationQuestion;
3838
use Symfony\Component\Console\Question\Question;
39-
use Symfony\Component\Finder\SplFileInfo;
4039

4140
/**
4241
* @author Javier Eguiluz <[email protected]>
@@ -100,20 +99,6 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
10099
return;
101100
}
102101

103-
$entityFinder = $this->fileManager->createFinder('src/Entity/')
104-
// remove if/when we allow entities in subdirectories
105-
->depth('<1')
106-
->name('*.php');
107-
$classes = [];
108-
/** @var SplFileInfo $item */
109-
foreach ($entityFinder as $item) {
110-
if (!$item->getRelativePathname()) {
111-
continue;
112-
}
113-
114-
$classes[] = str_replace(['.php', '/'], ['', '\\'], $item->getRelativePathname());
115-
}
116-
117102
$argument = $command->getDefinition()->getArgument('name');
118103
$question = $this->createEntityClassQuestion($argument->getDescription());
119104
$value = $io->askQuestion($question);
@@ -363,7 +348,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
363348

364349
// this is a normal field
365350
$data = ['fieldName' => $fieldName, 'type' => $type];
366-
if ('string' == $type) {
351+
if ('string' === $type) {
367352
// default to 255, avoid the question
368353
$data['length'] = $io->ask('Field length', 255, [Validator::class, 'validateLength']);
369354
} elseif ('decimal' === $type) {
@@ -467,23 +452,9 @@ private function printAvailableTypes(ConsoleStyle $io)
467452

468453
private function createEntityClassQuestion(string $questionText): Question
469454
{
470-
$entityFinder = $this->fileManager->createFinder('src/Entity/')
471-
// remove if/when we allow entities in subdirectories
472-
->depth('<1')
473-
->name('*.php');
474-
$classes = [];
475-
/** @var SplFileInfo $item */
476-
foreach ($entityFinder as $item) {
477-
if (!$item->getRelativePathname()) {
478-
continue;
479-
}
480-
481-
$classes[] = str_replace('/', '\\', str_replace('.php', '', $item->getRelativePathname()));
482-
}
483-
484455
$question = new Question($questionText);
485456
$question->setValidator([Validator::class, 'notBlank']);
486-
$question->setAutocompleterValues($classes);
457+
$question->setAutocompleterValues($this->doctrineHelper->getEntitiesForAutocomplete());
487458

488459
return $question;
489460
}

tests/fixtures/MakeEntityXmlMappingError/config/doctrine/User.orm.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
<id name="id" type="integer">
88
<generator strategy="AUTO" />
99
</id>
10-
<one-to-many field="avatars" target-entity="UserAvatar" mapped-by="user" />
1110
</entity>
12-
</doctrine-mapping>
11+
</doctrine-mapping>

0 commit comments

Comments
 (0)