문제

I've 2 tables with many-to-many relationship => users, roles.

I need to find all users THAT AREN'T IN ANY ROLE.

In SQL it can be done:

SELECT *
FROM `user`
WHERE `id` NOT
IN (
    SELECT `user_Id`
    FROM user_role
)

user_role is the joining table of many to many relationship from user and role.

How can obtain the same result with DQL?

UPDATE

The user_role table is an automated mapping from doctrine, if i try to use it, I get "entity not found"

Asking Better:

How can I do the same result with CreateQueryBuilder?

I can't be able to using the user_role table in my entity, because it's autogenerated with many-to-many relathionship annotation (or can I use it?).

도움이 되었습니까?

해결책

This should work:

SELECT u
FROM user u
LEFT JOIN u.roles r
WHERE r IS NULL

다른 팁

The way you are doing should get you the required results Or you can do a join something like this

SELECT *
FROM user u LEFT OUTER JOIN user_role ur
ON u.id = ur.user_Id
WHERE ur.user_Id IS NULL
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top