Doctrine2 QueryBuilder:如何过滤掉 OneToMany 计数为零的实体
Doctrine2 QueryBuilder: How to filter out entities having zero count for a OneToMany
在我的 Dymfony2 / Doctrine2 应用程序中,我在 object 和它的 children 之间有一个 oneToMany 关系。
我想 select 所有没有 children 的 object。
我遇到了各种错误:需要 SingleValuedAssociationField,无法在非结果变量上添加 Having sth,等等
$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();
我该如何解决这个问题?
有一个名为 SIZE()
的选择器,它应该可以解决问题。更多阅读 here.
尝试这样的事情:
$this
->createQueryBuilder('object')
->leftJoin('object.children', 'children')
->where('SIZE(object.children) = 0')
->getQuery()
->getResult();
在我的 Dymfony2 / Doctrine2 应用程序中,我在 object 和它的 children 之间有一个 oneToMany 关系。
我想 select 所有没有 children 的 object。
我遇到了各种错误:需要 SingleValuedAssociationField,无法在非结果变量上添加 Having sth,等等
$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();
我该如何解决这个问题?
有一个名为 SIZE()
的选择器,它应该可以解决问题。更多阅读 here.
尝试这样的事情:
$this
->createQueryBuilder('object')
->leftJoin('object.children', 'children')
->where('SIZE(object.children) = 0')
->getQuery()
->getResult();