From c589b5d01357afa009e1530e9ac55a5716839155 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Jan 2015 02:56:40 +0100 Subject: [PATCH] #1130 DDC-3300 - re-mapping discriminator column at runtime (based on modified class metadata) --- .../ORM/Mapping/ClassMetadataFactory.php | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index d70f02ffa..7dcbe1e48 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -30,6 +30,7 @@ use Doctrine\ORM\Events; use Doctrine\ORM\Id\BigIntegerIdentityGenerator; use Doctrine\ORM\Id\IdentityGenerator; use Doctrine\ORM\ORMException; +use Exception; use ReflectionException; /** @@ -70,6 +71,30 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory */ private $embeddablesActiveNesting = array(); + /** + * {@inheritDoc} + */ + protected function loadMetadata($name) + { + /* @var $loaded string[] */ + $loaded = parent::loadMetadata($name); + + foreach ($loaded as $className) { + /* @var $metadata ClassMetadata */ + $metadata = $this->getMetadataFor($className); + + if (! $metadata->discriminatorValue) { + foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) { + if ($metadata->name === $this->getMetadataFor($discriminatorClass)->getName()) { + $metadata->discriminatorValue = $discriminatorValue; + } + } + } + } + + return $loaded; + } + /** * @param EntityManagerInterface $em */ @@ -270,9 +295,9 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory if ( ! $class->discriminatorColumn) { throw MappingException::missingDiscriminatorColumn($class->name); } - } else if ($parent && !$class->reflClass->isAbstract() && !in_array($class->name, array_values($class->discriminatorMap))) { + } else if (! $class->reflClass->isAbstract() && !in_array($class->name, array_values($class->discriminatorMap))) { // enforce discriminator map for all entities of an inheritance hierarchy, otherwise problems will occur. - throw MappingException::mappedClassNotPartOfDiscriminatorMap($class->name, $class->rootEntityName); + //throw MappingException::mappedClassNotPartOfDiscriminatorMap($class->name, $class->rootEntityName); } } else if ($class->isMappedSuperclass && $class->name == $class->rootEntityName && (count($class->discriminatorMap) || $class->discriminatorColumn)) { // second condition is necessary for mapped superclasses in the middle of an inheritance hierarchy