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

Séb*_*ien 4 associations one-to-many query-builder symfony doctrine-orm

在我的Dymfony2/Doctrine2应用程序中,我在对象及其子对象之间存在oneToMany关系.

我想选择所有没有孩子的物品.

遇到了各种错误:期望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();
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

Art*_*iel 10

有一个叫做的选择器SIZE()应该可以解决问题.更多内容请阅读此处.

尝试这样的事情:

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