取消链接角色 - BjyAuthorize Doctirne

时间:2015-09-06 12:22:22

标签: doctrine-orm zend-framework2 zfcuser bjyauthorize

BjyAuthorize修改User实体并提供addRole()方法。这将接受角色对象并填充user_role_linker_table

如何将角色添加到用户后如何删除?

关联在User:

中设置
List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Collections.shuffle(list);
int[] loterryNumbers = list.subList(0, 5).stream().mapToInt(i -> i).toArray();

2 个答案:

答案 0 :(得分:0)

经过几个小时的奋斗,我想出了以下解决方案:

$userDetails = $em->getRepository('Application\Entity\UserDetails')->findOneBy(['id' => $data['user-details-id']]);
$user = $userDetails->getUser();

$roleRepo = $em->getRepository('Application\Entity\Role');
$roleResult = $roleRepo->findOneBy(['id' => $id]); //$id is the role to delete
$user->removeRole($roleResult);

$em->merge($user);
$em->flush();

在User实体中,我添加了方法:

    public function removeRole($role)
    {
        return $this->roles->removeElement($role);
    }

不确定这是否是BjyAuthorize的作者想要的方法,但它对我有用......

答案 1 :(得分:0)

对我来说很好看。只是想补充一点,您应首先检查角色是否包含您要删除的角色。

如:

public function removeRole($role)
{
    if (!$this->roles->contains($role))
    {
         return;
    }
    $this->roles->removeElement($role);
}