From 550694aa17e9786562c19cdd7eeaf1c92e0f49e5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 16 Jan 2015 16:46:29 +0100 Subject: [PATCH] #1130 DDC-3300 - rewording and clarifying logic that re-maps discriminator values when needed --- lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 0749b912ef9..ed8d82fb2ac 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -30,7 +30,6 @@ use Doctrine\ORM\Id\BigIntegerIdentityGenerator; use Doctrine\ORM\Id\IdentityGenerator; use Doctrine\ORM\ORMException; -use Exception; use ReflectionException; /** @@ -75,11 +74,10 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory * {@inheritDoc} */ protected function loadMetadata($name) - { - /* @var $loaded string[] */ +`` { $loaded = parent::loadMetadata($name); - array_map([$this, 'populateDiscriminatorValue'], array_map([$this, 'getMetadataFor'], $loaded)); + array_map([$this, 'resolveDiscriminatorValue'], array_map([$this, 'getMetadataFor'], $loaded)); return $loaded; } @@ -280,9 +278,6 @@ protected function validateRuntimeMetadata($class, $parent) if ( ! $class->discriminatorColumn) { throw MappingException::missingDiscriminatorColumn($class->name); } - } 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); } } 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 @@ -308,7 +303,7 @@ protected function newClassMetadataInstance($className) * * @throws MappingException */ - private function populateDiscriminatorValue(ClassMetadata $metadata) + private function resolveDiscriminatorValue(ClassMetadata $metadata) { if ($metadata->discriminatorValue || ! $metadata->discriminatorMap @@ -318,6 +313,7 @@ private function populateDiscriminatorValue(ClassMetadata $metadata) return; } + // minor optimization: avoid loading related metadata when not needed foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) { if ($discriminatorClass === $metadata->name) { $metadata->discriminatorValue = $discriminatorValue; @@ -326,6 +322,7 @@ private function populateDiscriminatorValue(ClassMetadata $metadata) } } + // iterate over discriminator mappings and resolve actual referenced classes according to existing metadata foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) { if ($metadata->name === $this->getMetadataFor($discriminatorClass)->getName()) { $metadata->discriminatorValue = $discriminatorValue;