我正在尝试过滤掉具有 super 管理员角色的管理员。为什么以下不起作用?
public function findAdmins()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('a')
->from('MyBundle:Admin', 'a')
->where($qb->expr()->notIn('ROLE_SUPER_ADMIN', 'a.roles'));
$result = $qb->getQuery()->execute();
return $result;
}
它会给我以下错误:
[Syntax Error] line 0, col 68: Error: Expected Literal, got 'a'
DQL 查询如下所示:
SELECT a FROM MyBundle:Admin a WHERE ROLE_SUPER_ADMIN NOT IN(a.roles)
角色本身不是一个实体。它只是一个字符串数组。
$roles = array('ROLE_ADMIN', 'ROLE_SUPER_ADMIN)'
最佳答案
尝试使用MEMBER OF
运算符
$qb
->select('a')
->from('MyBundle:Admin', 'a')
->where(':role NOT MEMBER OF ad.roles')->setParamete('role', 'ROLE_SUPER_ADMIN');
更新。
找到的唯一解决方案是使用 LIKE
检查数据库中的序列化字符串。 Details here
关于php - 如何在dql中搜索实体数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17915864/