How to execute a connection query using the Symfony query designer and Doctrine Query Builder

I have two objects that are linked through a 1: 1 ratio, for example: MyEntity.idRelatedEntity I want to create a Doctrine query where I can get data from MyEntity depending on the value from a specific column in RelatedEntity . Something like this (this does not work, of course):

 $entity = $em ->getRepository('MyBundle:RelatedEntity') ->createQueryBuilder('e') ->leftJoin('MyBundle:RelatedEntity', 'r') ->where('r.foo = 1') ->getQuery() ->getResult(); 

Any help would be greatly appreciated :)

+7
sql php symfony doctrine
source share
2 answers
 $entity = $em ->getRepository('MyBundle:MyEntity') ->createQueryBuilder('e') ->join('e.idRelatedEntity', 'r') ->where('r.foo = 1') ->getQuery() ->getResult(); 

Also the left join does not make sense here (due to the fact that the where clause will work as an internal join)

+15
source share

Please note that you must write this request in your MyEntityRepository

 public function getMyEntityWithRelatedEntity($parameter) { $query = $this->createQueryBuilder('e') ->leftJoin('e.relatedEntity', 'r') ->where('r.foo = :parameter') ->setParameter('parameter', $parameter) ->getQuery(); return $query->getResult(); } 

And then use it in your controller / service:

 $manager = $this->getDoctrine()->getManager(); $results = $manager->getRepository('MyBundle:MyEntity')->getMyEntityWithRelatedEntity(1); 
+12
source share

All Articles