按角色获取用户
Get users by role
我在一个实体(用户)中有一个实体(组)数组,在我的查询生成器中,我想按角色检索用户。
我正在尝试这样做:
public function findUsersByRole($role){
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where('u.roles LIKE :role')
->setParameter('role', $role);
return $qb->getQuery()->getResult();
}
但是我没有得到结果。
我也试过:
public function findUsersByRole($role)
{
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where(':role MEMBER OF u.roles')
->setParameter('role', $role);
return $qb->getQuery()->getResult();
}
但我收到错误消息:
[Semantical Error] line 0, col 62 near 'roles': Error: Invalid
PathExpression. Must be a CollectionValuedAssociationField.
我做错了什么?
角色存储在 JSON 类型的用户 table 字段角色字段中,如下所示:
[
"ROLE_USER",
"ROLE_ADMIN"
]
这是用户class的函数,在数据库中存储用户角色:
public function setRoles(array $roles){
$this->roles = $roles;
}
这是我正在编写的单元测试,用于测试 testFindUsersByRole 函数:
public function testFindUsersByRole() {
$user1 = $this->createUser(1, "Developer", "John", "Smith", "smith@test.com");
$user2 = $this->createUser(2, "Developer", "Joe", "Black", "joe@test.com");
$user3 = $this->createUser(3, "Developer", "Jack", "Blade", "blade@test.com");
$roles1 = ['ROLE_ADMIN','ROLE_USER'];
$roles2 = ['ROLE_USER'];
$user1->setRoles($roles1);
$user2->setRoles($roles2);
$response = $this->userRepository->findUsersByRole('ROLE_ADMIN');
$this->assertCount(1, $response);
$this->purge([$user1, $user2, $user3]);
}
我发现我做错了什么。
在我的单元测试中,我没有在分配角色后将用户实体保存在数据库中。
所以我已经修复了这样的单元测试。
public function testFindUsersByRole() {
$user1 = $this->createUser(1, "Developer", "John", "Smith", "smith@test.com");
$user2 = $this->createUser(2, "Developer", "Joe", "Black", "joe@test.com");
$user3 = $this->createUser(3, "Developer", "Jack", "Blade", "blade@test.com");
$roles1 = ['ROLE_ADMIN','ROLE_USER'];
$roles2 = ['ROLE_USER'];
$user1->setRoles($roles1);
$user2->setRoles($roles2);
$this->entityManager->persist($user1);
$this->entityManager->persist($user2);
$this->entityManager->flush();
$response = $this->userRepository->findUsersByRole('ROLE_ADMIN');
$this->assertCount(1, $response);
$this->purge([$user1, $user2, $user3]);
}
这个函数就是有效的函数:
public function findUsersByRole($role){
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%"'.$role.'"%');
return $qb->getQuery()->getResult();
}
我在一个实体(用户)中有一个实体(组)数组,在我的查询生成器中,我想按角色检索用户。
我正在尝试这样做:
public function findUsersByRole($role){
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where('u.roles LIKE :role')
->setParameter('role', $role);
return $qb->getQuery()->getResult();
}
但是我没有得到结果。
我也试过:
public function findUsersByRole($role)
{
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where(':role MEMBER OF u.roles')
->setParameter('role', $role);
return $qb->getQuery()->getResult();
}
但我收到错误消息:
[Semantical Error] line 0, col 62 near 'roles': Error: Invalid PathExpression. Must be a CollectionValuedAssociationField.
我做错了什么?
角色存储在 JSON 类型的用户 table 字段角色字段中,如下所示:
[
"ROLE_USER",
"ROLE_ADMIN"
]
这是用户class的函数,在数据库中存储用户角色:
public function setRoles(array $roles){
$this->roles = $roles;
}
这是我正在编写的单元测试,用于测试 testFindUsersByRole 函数:
public function testFindUsersByRole() {
$user1 = $this->createUser(1, "Developer", "John", "Smith", "smith@test.com");
$user2 = $this->createUser(2, "Developer", "Joe", "Black", "joe@test.com");
$user3 = $this->createUser(3, "Developer", "Jack", "Blade", "blade@test.com");
$roles1 = ['ROLE_ADMIN','ROLE_USER'];
$roles2 = ['ROLE_USER'];
$user1->setRoles($roles1);
$user2->setRoles($roles2);
$response = $this->userRepository->findUsersByRole('ROLE_ADMIN');
$this->assertCount(1, $response);
$this->purge([$user1, $user2, $user3]);
}
我发现我做错了什么。
在我的单元测试中,我没有在分配角色后将用户实体保存在数据库中。
所以我已经修复了这样的单元测试。
public function testFindUsersByRole() {
$user1 = $this->createUser(1, "Developer", "John", "Smith", "smith@test.com");
$user2 = $this->createUser(2, "Developer", "Joe", "Black", "joe@test.com");
$user3 = $this->createUser(3, "Developer", "Jack", "Blade", "blade@test.com");
$roles1 = ['ROLE_ADMIN','ROLE_USER'];
$roles2 = ['ROLE_USER'];
$user1->setRoles($roles1);
$user2->setRoles($roles2);
$this->entityManager->persist($user1);
$this->entityManager->persist($user2);
$this->entityManager->flush();
$response = $this->userRepository->findUsersByRole('ROLE_ADMIN');
$this->assertCount(1, $response);
$this->purge([$user1, $user2, $user3]);
}
这个函数就是有效的函数:
public function findUsersByRole($role){
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where('u.roles LIKE :roles')
->setParameter('roles', '%"'.$role.'"%');
return $qb->getQuery()->getResult();
}