Injecting the Service Manager to Build a Doctrine Repository in ZF2

前端 未结 2 796
不知归路
不知归路 2021-02-11 10:33

How do I inject the service manager into a Doctrine repository to allow me to retrieve the Doctrine Entity Manager?

I using the ZF2-Commons DoctrineORMModule and are try

2条回答
  •  情书的邮戳
    2021-02-11 11:30

    As long as you extend the Doctrine\ORM\EntityRepository, you have immediate access to the entity manager by calling EntityRepository::getEntityManager() or the $_em attribute. The inheritence from the Doctrine\ORM\EntityRepository class allow you to do so.

    Your method should now look like this:

    public function getUserApptsByDate()
    {
        $dql = "SELECT a FROM Appointment a";
        $em = $this->getEntityManager();// Or $em=$this->_em;
        $query = $em()->createQuery($dql);
        return $query->getResult();
    }
    

    I always keep in mind that access to my data should go from the web front (Zend MVC, Service Manager) to the persistence layer (Doctrine). My persistence (entities, repositories...) layer should not refer to the web front or neither know that it exists. If my system is doing the inverse at some level, then probably I'm doing something wrong.

    Happy end of year

提交回复
热议问题