Is there a simple way to inject a dependency into every repository instance in Doctrine2 ?
I have tried listening to the loadClassMetadata event and usi
I've just define my own RepositoryFactory class
my_service.orm_repository.robo_repository_factory, with include @service_container injectionAnd add check and set container service, for instance:
private function createRepository(EntityManagerInterface $entityManager, $entityName)
{
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryClassName = $metadata->customRepositoryClassName
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
$result = new $repositoryClassName($entityManager, $metadata);
if ($result instanceof ContainerAwareInterface) {
$result->setContainer($this->container);
}
return $result;
}
Create compiler class
public function process(ContainerBuilder $container)
{
$def = $container->getDefinition('doctrine.orm.configuration');
$def->addMethodCall(
'setRepositoryFactory', [new Reference('robo_doctrine.orm_repository.robo_repository_factory')]
);
}
After that any EntityRepository with ContainerAwareInterface has @service_container