Doctrine2 get object without relations

后端 未结 1 1325
遥遥无期
遥遥无期 2021-01-26 12:08

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

I would like to find roles without users.

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

1条回答
  •  囚心锁ツ
    2021-01-26 12:39

    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();
    

    0 讨论(0)
提交回复
热议问题