Skip to content

Commit

Permalink
doctrine#1130 DDC-3300 - re-mapping discriminator column at runtime (…
Browse files Browse the repository at this point in the history
…based on modified class metadata)
  • Loading branch information
Ocramius committed Jan 16, 2015
1 parent 882b7dd commit 32d2baa
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Doctrine\ORM\Id\BigIntegerIdentityGenerator;
use Doctrine\ORM\Id\IdentityGenerator;
use Doctrine\ORM\ORMException;
use Exception;
use ReflectionException;

/**
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -266,9 +291,9 @@ protected function validateRuntimeMetadata($class, $parent)
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
Expand Down

0 comments on commit 32d2baa

Please sign in to comment.