Injecting dependency into entity repository

后端 未结 6 1498
有刺的猬
有刺的猬 2020-12-13 04:46

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

6条回答
  •  天命终不由人
    2020-12-13 05:08

    Problem is that repository classes are not part of the Symfony2 codebase as they are part of Doctrine2, so they do not take advantage of the DIC; this is why you can't go for injection in one place for all repositories.

    I would advice you to use a different approach. For example you can create a service layer on top of the repositories and actually inject the class you want through a factory in that layer.

    Otherwise you could also define repositories as services this way:

    
      entity_name
      entity_manager_name
      
          your_argument
      
    
    

    A solution that could centralize the set method call is to write a DIC tag and a compiler pass to handle it and tag all repository services.

提交回复
热议问题