2

This is relevant to legacy project written in Symfony 2.2.1, doctrine 2.3.3.

I need to access DIC @service in one of EntityRepository classes.

Am i able to inject this service, or container into it via some event listeners or somehow else?

I dont want to inject it into Entity, but particular EntityRepository.

palmic
  • 1,846
  • 2
  • 20
  • 30
  • 2.2? Yikes! What version of Doctrine are you using? Newer versions have a RepositoryFactory which can be overridden and tweaked to inject additional services into your repositories. So check under vendor/doctrine/orm and see if you have the class. – Cerad Feb 23 '18 at 14:59
  • Yo' this is very legacy project and it will be deprecated soon :-) . So i am not looking for clean solution really. To be honest this is part of decommissioning the project. Description updated. – palmic Feb 23 '18 at 15:02
  • 1
    Fair enough, @Mocrates answer will work assuming you can pull the repo from the container. Otherwise your best bet might to simply access the global $kernel->getContainer() method. – Cerad Feb 23 '18 at 15:30
  • Thanks @Cerad i found it in debugger before you wrote it here... – palmic Feb 23 '18 at 15:39

2 Answers2

2

You can try to declare your Repo as service and add calls to inject your other service

#services.yml
entity.repo :
   class: 'YourRepoNamespace'
   factory-service: 'doctrine.orm.default_entity_manager'
   factory-method: 'getRepository'
   arguments: ['YourEntityNamespace']
   calls:
     - ['setOtherService', ['@other_service']] 
Mocrates
  • 209
  • 1
  • 5
  • OK, i found it in comments to my question from @Cerad - the problem is i need this to work without any change of instantiating the repo - this way i am dealing with it basically because its used at many places in project and its pulled as $em->getRepository('RepoName'). I simply cannot secure it will be got via DIC. Thank you anyway! – palmic Feb 23 '18 at 15:42
0

Ok, this is really not clean solution, but it works even if you want to keep getting the repo by $entityManaget->getRepository(). Since the project will be deprecated soon, its good enough...

Just add this DIC getter to your EntityRepository class and you are able to get any DIC service in any Repository in project...

protected function getDICService($serviceName)
{
    /** @var \AppKernel $kernel */
    $kernel = $GLOBALS['kernel'];
    $container = $kernel->getContainer();
    return $container->get($serviceName);
}
palmic
  • 1,846
  • 2
  • 20
  • 30
  • Can I suggest you accept the other answer as future users reading this question should prefer the other solution. But your answer is good for your very specific problem – goto Feb 23 '18 at 15:49
  • Hello @goto. I dont have confirmed it will work, lets make compromise and let this question unsolved to let users vote for best answer. Its short enough for anybody to choose which solution is better for him. – palmic Feb 23 '18 at 15:56
  • @goto The problem is that the other answer does not work for $entityManaget->getRepository() which is far more commonly used than making a repository service. The best up to date solution would use a custom Doctrine RepositoryFactory. – Cerad Feb 23 '18 at 19:17
  • Cerad <- exactly. I just want to wait for users to vote between approaches and then, after some months if they will pick @Mocrates response, which is more modern, but not succesfull for my use case (and i supported it by +) i will solve it... – palmic Feb 23 '18 at 23:30