[英]Symfony2 doctrine: How to select users that are not in groups with doctrine query builder
I have symfony2 and fos user bundle installed and I'm trying to reproduce this MySql query in Symfony2 doctrine query builder. 我已经安装了symfony2和fos用户捆绑包,并且试图在Symfony2学说查询构建器中重现此MySql查询。
This query should select users not belonging to listed groups. 此查询应选择不属于列出的组的用户。
SELECT * FROM
fos_user_user
left join
fos_user_user_group on fos_user_user.id = fos_user_user_group.user_id
and fos_user_user_group.group_id in (1,2,3,4,5,6,7,8)
where
fos_user_user_group.user_id is null
Doctrine manual gives example that checks for user belonging to single group, but no reference when groupId is array of several group ID's . Doctrine手册给出了检查属于单个组的用户的示例,但当groupId 是多个组ID的数组时,则没有引用。 :groupId is not integer, doctrine gives an error.
:groupId不是整数,教义给出错误。
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE :groupId MEMBER OF u.groups');
$query->setParameter('groupId', $group);
$ids = $query->getResult();
Taken from 14. Doctrine Query Language 取自14.学说查询语言
Mysql scheme is default FOS User bundle. Mysql方案是默认的FOS用户捆绑包。 In simplified manner it looks like this:
以简化的方式看起来像这样:
CREATE TABLE IF NOT EXISTS `fos_user_user` (
`id` int(11) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `fos_user_group` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`roles` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)',
`deletedAt` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `fos_user_user_group` (
`user_id` int(11) NOT NULL,
`group_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You can add a count
to the query, and add a where count = 0
or having count = 0
after that. 您可以向查询中添加一个
count
,然后添加一个where count = 0
或之后的having count = 0
。 This should return the users without a group. 这应该返回没有组的用户。
Something like this: 像这样:
$query = $em->createQuery('SELECT u.id, COUNT(u.groups) as groupCount FROM CmsUser u WHERE groupCount = 0');
$ids = $query->getResult();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.