Created
April 16, 2012 08:49
-
-
Save juzna/2397149 to your computer and use it in GitHub Desktop.
Doctrine listener: Updates custom repository class definition if explicitly defined in parent class
This file contains 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 | |
use \Doctrine\ORM\Event\LoadClassMetadataEventArgs; | |
use Nette, | |
Doctrine, | |
Doctrine\ORM\Events; | |
/** | |
* Updates custom repository class definition if explicitly defined in parent class | |
* | |
* @author Jan Dolecek <juzna.cz@gmail.com> | |
*/ | |
class RepositoryClassListener implements \Doctrine\Common\EventSubscriber { | |
/** @var string */ | |
private $defaultCustomRepository; | |
public function __construct($defaultRepo = null) { | |
$this->defaultCustomRepository = $defaultRepo; | |
} | |
function getSubscribedEvents() { | |
return array( | |
Events::loadClassMetadata, | |
); | |
} | |
public function loadClassMetadata(LoadClassMetadataEventArgs $args) { | |
$md = $args->getClassMetadata(); | |
if(!empty($md->customRepositoryClassName)) return; // already defined | |
foreach($md->parentClasses as $parentClass) { | |
$parentMd = $args->getEntityManager()->getClassMetadata($parentClass); | |
if($parentMd->customRepositoryClassName) { | |
$md->customRepositoryClassName = $parentMd->customRepositoryClassName; | |
return; | |
} | |
} | |
// application default | |
$md->customRepositoryClassName = $this->defaultCustomRepository; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment