如何使用Symfony和Doctrine查询生成器执行连接查询 [英] How to perform a join query using Symfony and Doctrine Query Builder
本文介绍了如何使用Symfony和Doctrine查询生成器执行连接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MyEntity.idRelatedEntity
我想创建一个Doctrine查询,在那里我可以检索来自
MyEntity
的数据取决于 RelatedEntity
中某列的值。这样的东西(当然不行): $ entity = $ em
- > getRepository ('MyBundle:RelatedEntity')
- > createQueryBuilder('e')
- > leftJoin('MyBundle:RelatedEntity','r')
- > where('r .foo = 1')
- > getQuery()
- > getResult();
任何帮助将不胜感激:)
解决方案
$ entity = $ em
- > getRepository('MyBundle:MyEntity')
- > createQueryBuilder 'e')
- > join('e.idRelatedEntity','r')
- >其中('r.foo = 1')
- > getQuery()
- > getResult();
此外,左连接在这里也没有任何意义(因为where子句会使内部连接起作用)
I have two entities which are connected through a 1:1 relationship, e.g: MyEntity.idRelatedEntity
I want to create a Doctrine query where I can retrieve data from MyEntity
depending on a value from a certain column in RelatedEntity
. Something like this (it doesn't work of course):
$entity = $em
->getRepository('MyBundle:RelatedEntity')
->createQueryBuilder('e')
->leftJoin('MyBundle:RelatedEntity', 'r')
->where('r.foo = 1')
->getQuery()
->getResult();
Any help would be much appreciated :)
解决方案
$entity = $em
->getRepository('MyBundle:MyEntity')
->createQueryBuilder('e')
->join('e.idRelatedEntity', 'r')
->where('r.foo = 1')
->getQuery()
->getResult();
Also left join makes no sense here (because of where clause that will make it work like inner join)
这篇关于如何使用Symfony和Doctrine查询生成器执行连接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文