2017-09-04 60 views
0

我有一个存储库,我需要使用另一个持有不同连接的实体管理器。这个例子是不行的,因为$this->getEntityManager('anotherConnection')得到它使用当前仓库同一经理:单个存储库中的多个实体管理器

class BlahRepository extends \Doctrine\ORM\EntityRepository 
{ 
    public function find($id) 
    { 
     $qb = $this->createQueryBuilder('a') 
       ->select('a') 
       ->where('a.id = :id') 
       ->setParameter('id', $id); 
     $result = $qb->getQuery()->getResult(); 

     $repositoryFromAnotherManager = $this->getEntityManager('anotherConnection') 
       ->getRepository('Bundle:Entity'); 

     foreach ($result as $value) { 
      $something = $repositoryFromAnotherManager->getTranslatedSomething($value->getSomething()); 
      $value->setTranslatedSomething($something); 
     } 

     return $result; 
    } 
} 

我的问题是:是否有可能获得其持有仓库等有关的其他实体管理器。注意:此库不是通过注入来调用的,因此在services.yml中更改getRepository工厂不是解决方案。

+2

我认为你最好的选择是制作一个自定义服务并将两个存储库注入它。我能想到的唯一的另一种方式是覆盖原则库工厂服务。你可以搜索如何做到这一点。 – Cerad

+0

我想我会使用服务,谢谢 – Eimsas

回答

0

我使用的解决方案是:创建管理器服务并注入到2个存储库。当然,如果有人将库作为服务注入,那么你也可以重写工厂原则存储库服务。感谢@Cerad

相关问题