Skip to content

Commit

Permalink
bug #197 Preserve existing class metadata (ro0NL)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weaverryan committed Jun 13, 2018
2 parents 5df0071 + 79ba86a commit 0e8d40d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Bundle\MakerBundle\Doctrine;

use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
Expand Down Expand Up @@ -125,11 +127,21 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected

/** @var EntityManagerInterface $em */
foreach ($this->getRegistry()->getManagers() as $em) {
$cmf = $em->getMetadataFactory();

if ($disconnected) {
try {
$loaded = $cmf->getAllMetadata();
} catch (MappingException $e) {
$loaded = $cmf instanceof AbstractClassMetadataFactory ? $cmf->getLoadedMetadata() : [];
}

$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
} else {
$cmf = $em->getMetadataFactory();

foreach ($loaded as $m) {
$cmf->setMetadataFor($m->getName(), $m);
}
}

foreach ($cmf->getAllMetadata() as $m) {
Expand Down

0 comments on commit 0e8d40d

Please sign in to comment.