Skip to content

Commit 0e8d40d

Browse files
committed
bug #197 Preserve existing class metadata (ro0NL)
This PR was squashed before being merged into the 1.0-dev branch (closes #197). Discussion ---------- Preserve existing class metadata Hi, Im facing an issue related to `make:entity`. I noticed it uses `DisconnectedClassMetadataFactory` which at this point basically forgets about existing metadata (metadata that on itself is loaded during the `loadClassMetadata` event). Real life example is e.g. https://github.com/msgphp/symfony-demo-app/blob/b9536243f4a0803b36acfef839ac0c3559a91bd6/src/Entity/User/UserRole.php#L11 where the `roles` field is mapped during `loadClassMetadata`. Currently its mapping is not loaded, causing an invalid field override here. This solves it. Commits ------- 79ba86a Preserve existing class metadata
2 parents 5df0071 + 79ba86a commit 0e8d40d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Doctrine/DoctrineHelper.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Doctrine;
1313

14+
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
1415
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
1516
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
1617
use Doctrine\ORM\EntityManagerInterface;
1718
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
19+
use Doctrine\ORM\Mapping\MappingException;
1820
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
1921
use Symfony\Bridge\Doctrine\ManagerRegistry;
2022
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
@@ -125,11 +127,21 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
125127

126128
/** @var EntityManagerInterface $em */
127129
foreach ($this->getRegistry()->getManagers() as $em) {
130+
$cmf = $em->getMetadataFactory();
131+
128132
if ($disconnected) {
133+
try {
134+
$loaded = $cmf->getAllMetadata();
135+
} catch (MappingException $e) {
136+
$loaded = $cmf instanceof AbstractClassMetadataFactory ? $cmf->getLoadedMetadata() : [];
137+
}
138+
129139
$cmf = new DisconnectedClassMetadataFactory();
130140
$cmf->setEntityManager($em);
131-
} else {
132-
$cmf = $em->getMetadataFactory();
141+
142+
foreach ($loaded as $m) {
143+
$cmf->setMetadataFor($m->getName(), $m);
144+
}
133145
}
134146

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

0 commit comments

Comments
 (0)