symfony - 以关系数量为条件的教义查询
问题描述
我有两个实体:Location
和Program
. 他们有一个多对多的关系。程序具有属性$status
,可以是'published'
或'unpublished'
。
现在我希望运行一个查询(使用教义查询生成器)来返回我的所有Location
与至少 10 个Program
实体有关系$status
的实体'published'
。
到目前为止我的代码
public function findLocationsOfPublishedPrograms()
{
$qb = $this->createQueryBuilder('l')
->innerJoin('l.programs', 'p')
->andWhere('p.status = \'published\'')
->getQuery()->getResult();
return $qb;
}
此查询返回Location
至少有一个已发布程序的所有实体。但是有什么办法可以设置一个条件,只返回Location
至少有10 个已发布程序的实体?
解决方案
You could innerJoin WITH p.status = published. Then addWhere with a count of p.* >= 10
Examples of innerJoin with conditions are in the docs: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#working-with-querybuilder
推荐阅读
- ios - Firebase 观察者值影响所有集合视图单元格
- java - Eclipse 不部署 Maven 依赖项
- .htaccess - 将特定 URL 重定向到另一个子域上的另一个 URL
- javascript - 使用 chart.js 在一页中显示多个图表
- python - PyMonzo 库和使用 Raspberry Pi 3 的 Adafruit_Neopixel 库之间的冲突
- java - Scanner 对象在 while 循环中只执行一次
- python - 如何将纪元时间转换为时间
- azure - Powershell循环直到输出为一行
- c++ - 运行相同解决方案的另一个项目的项目
- ios - IONIC 3 本地通知在带有 FCM 的 IOS 中不起作用