简体   繁体   English

Doctrine2 QueryBuilder:如何过滤掉OneToMany的零计数实体

[英]Doctrine2 QueryBuilder: How to filter out entities having zero count for a OneToMany

In my Dymfony2 / Doctrine2 application, I have a oneToMany relation between an object and its children. 在我的Dymfony2 / Doctrine2应用程序中,我在对象及其子对象之间有一个oneToMany关系。

I want to select all objects which have no children. 我想选择所有没有孩子的物品。

I'm stuck with various errors: SingleValuedAssociationField expected, Cannot add Having sth on non result variable , etc. 遇到了各种错误: 期望SingleValuedAssociationField,无法添加非结果变量等等。

$queryBuilder = $this
    ->createQueryBuilder('object')
    ->leftJoin('object.children', 'children')
    ->andWhere('children IS NULL')
    // tested with a parameter, with addselect COUNT(children) and 0 condition, etc.
    ->getQuery()
    ->getResult();

How can I solve this? 我怎么解决这个问题?

There is a selector called SIZE() , which should do the trick. 有一个名为SIZE()的选择器,应该可以做到这一点。 More to read here. 更多内容请阅读此处。

Try something like this: 尝试这样的事情:

$this
    ->createQueryBuilder('object')
    ->leftJoin('object.children', 'children')
    ->where('SIZE(object.children) = 0')
    ->getQuery()
    ->getResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM