Injecting dependency into entity repository

后端 未结 6 1495
有刺的猬
有刺的猬 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:21

    Since Symfony 3.3+ and 2017 you can make use of services.


    Instead of other proposed solutions here that lead to:

    • hacking the repository factory
    • making service configuration in YAML
    • and creating a lot of boilerplate code that will hunt you down later

    You can do it...


    Clean Way - Dependency via Constructor Injection like in any other Service

    repository = $entityManager->getRepository(Post::class);
    
            $this->yourOwnDependency = $yourOwnDependency
        }
    }
    


    Read More in the Post

    You can read more detailed tutorial with clear code examples in How to use Repository with Doctrine as Service in Symfony post.

提交回复
热议问题