Skip to content

Instantly share code, notes, and snippets.

@mikeemoo
Created May 29, 2012 15:24
Show Gist options
  • Select an option

  • Save mikeemoo/2829041 to your computer and use it in GitHub Desktop.

Select an option

Save mikeemoo/2829041 to your computer and use it in GitHub Desktop.
InheritanceListener
<?php
namespace Lowpress\CmsBundle\Doctrine;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
class InheritanceListener implements \Doctrine\Common\EventSubscriber {
private $driver;
public function getSubscribedEvents()
{
return array( Events::loadClassMetadata );
}
public function __construct( \Doctrine\ORM\EntityManager $db)
{
$this->driver = $db->getConfiguration()->getMetadataDriverImpl();
}
public function loadClassMetadata( \Doctrine\ORM\Event\LoadClassMetadataEventArgs $event )
{
$currentClassName = $event->getClassMetadata()->name;
$pathChildren = array();
if ($currentClassName == "Lowpress\CmsBundle\Entity\Path")
{
foreach ($this->driver->getAllClassNames() as $entityClass){
$reflectedEntity = new \ReflectionClass($entityClass);
if ($reflectedEntity->isSubclassOf($currentClassName) || $entityClass == $currentClassName)
{
$pathChildren[] = $entityClass;
}
}
if (!empty($pathChildren))
{
$map = array_combine($pathChildren, $pathChildren);
$event->getClassMetadata()->setDiscriminatorColumn(array("name" => "discr", "type" => "string", "length" => 1024));
$event->getClassMetadata()->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_JOINED);
$event->getClassMetadata()->setDiscriminatorMap($map);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment