问题描述:

I have two models: Users and Groups with many-to-many relationship. So I need select all users, which hasn't group. It must me realize by DQL.

Ok, something more information. I work in symfony 2 with sonata admin bundle. In my list view I want to show users which has no relationship with group. For example: When admin in admin panel create user, he choose group for this user. But when user register from frontend, this user has no any groups. So, I want to see all this users without groups. In sonata admin bundle in Admin classs I can inherit createQuery method, where I can write my own DQL query. As an example:

public function createQuery($context = 'list')

{

$query = parent::createQuery($context);

$query->andWhere('o.admin = TRUE');

$query->orderBy('o.loginDate', 'DESC');

return $query;

}

So, in my case I want choose users without groups, something like this:

public function createQuery($context = 'list')

{

$query = parent::createQuery($context);

$query->addSelect('g');

$query->leftJoin('o.groupList', 'g');

// here I need some DQL which chose users without groups

return $query;

}

So, now can anybody help?

网友答案:

If you have a relationship into user, like a groups property being an ArrayCollection, then you could simply get users where count(u.groups) = 0 or something like that?

相关阅读:
Top