Skip to content

Instantly share code, notes, and snippets.

@stevelacey
Forked from benlumley/Config.yml
Last active May 10, 2024 15:05
Show Gist options
  • Save stevelacey/4444247 to your computer and use it in GitHub Desktop.
Save stevelacey/4444247 to your computer and use it in GitHub Desktop.
services:
application.child1bundle.doctrine.listener.inheritance_mapping:
class: Application\BaseBundle\Doctrine\Listener\InheritanceMapping
arguments: [ %application.child1bundle.discriminator_mapping% ]
tags:
- { name: doctrine.event_listener, event: loadClassMetadata }
parameters:
application.child1bundle.discriminator_mapping:
-
parent: Application\ParentBundle\Entity\Category
child: Application\Child1Bundle\Entity\Category
-
parent: Application\ParentBundle\Entity\Product
child: Application\Child1Bundle\Entity\Product
services:
application.child2bundle.doctrine.listener.inheritance_mapping:
class: Application\BaseBundle\Doctrine\Listener\InheritanceMapping
arguments: [ %application.child2bundle.discriminator_mapping% ]
tags:
- { name: doctrine.event_listener, event: loadClassMetadata }
parameters:
application.child2bundle.discriminator_mapping:
-
parent: Application\ParentBundle\Entity\SomethingElse
child: Application\Child2Bundle\Entity\SomethingElse
<?php
namespace Application\BaseBundle\Doctrine\Listener;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
class InheritanceMapping
{
protected $mappings;
public function __construct($mappings)
{
$this->mappings = $mappings;
}
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
if (empty($this->mappings)) {
return;
}
$metadata = $eventArgs->getClassMetaData();
$class = $metadata->getName();
foreach ($this->mappings as $map) {
if ($class == $map['parent']) {
$metadata->addDiscriminatorMapClass($map['child'], $map['child']);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment