Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a repository in a different repository symfony2

Tags:

php

symfony

I have a question about repositories: Is it possible to use

$em = $this->getDoctrine()
                ->getEntityManager();
     $range = $em->getRepository('***Bundle:entityA')
                ->find($id);

in the repository of an entityB ????

like image 573
user2269869 Avatar asked Sep 20 '25 07:09

user2269869


2 Answers

In your repository class you already have access to entity manager, so you just need to do:

$this->getEntityManager()->getRepository('***Bundle:entityA')->find($id)
like image 178
brpaz Avatar answered Sep 22 '25 03:09

brpaz


I'd recommend the following:

Case A: You need to query 2 Entities, no relation to each other. Use 2 repositories, 2 queries

$range1 = $em->getRepository('***Bundle:entityA')->find($id);
$range2 = $em->getRepository('***Bundle:entityB')->find($id);

Case B: You need to query 2 Entities, related to each other, or depending on each other. Use 1 Repository, write a custom repository function, join them, or select on multiple tables

$range = $em->getRepository('***Bundle:entityA')->findAjoinedB();

 class EntityArepository extends EntityRepository
 {

   public function findAjoinedB(){
       $qb = $this->createQueryBuilder('entityA')
             ->leftJoin('entityA.entityB_id', 'entityB')
             ->where(....
             ....;

       return $qb->getQuery()->getResult();
   }
 }
like image 21
DerStoffel Avatar answered Sep 22 '25 01:09

DerStoffel