damianociarla / DCSDynamicDiscriminatorMapBundle

DCSDynamicDiscriminatorMapBundle simplifies the use of Doctrine Single Table and Class Table Inheritance mapping strategy in Symfony2
MIT License
12 stars 9 forks source link

Overwrite DiscriminatorMap or add one at a time #4

Open jbouzekri opened 10 years ago

jbouzekri commented 10 years ago

Hi,

I was wondering why you use AnnotationReader in your DoctrineEventListener. Moreover this solution works only when using annotation.

The ClassMetadataInfo has a public discriminatorMap attribute. You can get all loaded map at runtime and you can call addDiscriminatorMapClass to add each mapping from the config.yml.

So the code would like this :

class DoctrineEventListener
{
    ...

    public function loadClassMetadata(LoadClassMetadataEventArgs $event)
    {
        $metadata = $event->getClassMetadata();
        $class = $metadata->getReflectionClass();

        foreach ($this->mapping as $entityName => $map) {
            if ($class->getName() == $map['entity']) {
                // First solution
                $discriminatorMap = array_merge($metadata->discriminatorMap, $map['map']);
                $discriminatorMap = array_merge($discriminatorMap, array($entityName => $map['entity']));
                $metadata->setDiscriminatorMap($discriminatorMap);

                // Second solution
                foreach ($map['map'] as $name => $entityClass) {
                        $metadata->addDiscriminatorMapClass($name, $entityClass);
                }
            }
        }
    }
}

I don't think you should automaticaly add the mapping with the entity name :

$discriminatorMap = array_merge($discriminatorMap, array($entityName => $map['entity']));

Best regards

damianociarla commented 10 years ago

Hi @jbouzekri, I think you have a valid point of view and I’m ok with this change. Please send me a pull-request so I can try out the changes and tag a new version.

Thanks!!!