首页 > 解决方案 > 以关系数量为条件的教义查询

问题描述

我有两个实体:LocationProgram. 他们有一个多对多的关系。程序具有属性$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 个已发布程序的实体?

标签: symfonydoctrine-orm

解决方案


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


推荐阅读