Skip to content

Instantly share code, notes, and snippets.

@juzna
Created April 16, 2012 08:49
Show Gist options
  • Save juzna/2397149 to your computer and use it in GitHub Desktop.
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
<?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