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 ????
In your repository class you already have access to entity manager, so you just need to do:
$this->getEntityManager()->getRepository('***Bundle:entityA')->find($id)
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With