教义2获取没有关系的对象 [英] Doctrine2 get object without relations

查看:60
本文介绍了教义2获取没有关系的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户只有一个角色。
一个角色有零个或多个用户。

An user has one role. A role has zero or many users.

我想查找没有用户的角色。

I would like to find roles without users.

我需要使用不使用IN或NOT IN 的查询

I need to have this query without using IN or NOT IN

我尝试使用join:

$qb = $this->createQueryBuilder('role');
$qb
    ->leftJoin('role.users', 'users')
    ->where('users IS NULL')

不加入

$qb = $this->createQueryBuilder('role');
$qb
    ->where('role.users IS NULL')

id:

$qb = $this->createQueryBuilder('role');
$qb
    ->leftJoin('role.users', 'users')
    ->where('users.role != role')

您还有其他想法吗?除了使用IN / NOT IN查询,我别无选择吗?

Do you have other ideas? Do I have no other choices than to use IN / NOT IN queries?

预先感谢

推荐答案

您可以使用计数查询来查找没有任何用户的角色

You can find roles that don't have any users by using a count query

$qb = $this->createQueryBuilder('role');
$qb ->addSelect('COUNT(users.id) AS total_users')
    ->leftJoin('role.users', 'users')
    ->groupBy('role.id')
    ->having('total_users = 0')
    ->getQuery()->getResult();

这篇关于教义2获取没有关系的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆