本文介绍了Doctrine2通过QueryBuilder获取manyToMany关联的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家。
我有2个实体城市和POI。 Mapping如下所示:
everyone.I have 2 entities City and POI. Mapping looks like this:
class City {
/**
* @ORM\ManyToMany(targetEntity="POI", mappedBy="cities")
* @ORM\OrderBy({"position" = "ASC"})
*/
protected $pois;
和
class POI {
/**
* @ORM\ManyToMany(targetEntity="City", inversedBy="pois")
* @ORM\JoinTable(name="poi_cities")
*/
protected $cities;
我想使用QueryBuilder获取与某个城市至少有1个关联的所有POI。我应该可以使用exists()函数,但是我不安静知道如何。
I would like to fetch all POIs that have at least 1 association with some City using QueryBuilder. I should probably use exists() function but I don't quiet know how.
推荐答案
你必须左加入
,并检查城市
是否为空。
You'd have to Left join
them and check if cities
is null.
$qb->select('p', 'c')
->from('AcmeDemoBundle:POI', 'p')
->leftJoin('p.cities', 'c')
->where('c IS NOT NULL');
我没有测试过,但我希望它给你一般的方向。您可以从。
I haven't tested it, but I hope it gives you the general direction. You can read more about the QueryBuilder
from here.
这篇关于Doctrine2通过QueryBuilder获取manyToMany关联的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!