Created
May 29, 2012 15:24
-
-
Save mikeemoo/2829041 to your computer and use it in GitHub Desktop.
InheritanceListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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