【问题标题】:Add roles in FOSUserBundle在 FOSUserBundle 中添加角色
【发布时间】:2015-06-20 16:06:53
【问题描述】:

我和发这个帖子的论坛用户一样:

我实现了 FOSUserBundle,并且我想添加从表中获取的 RegisrationFormType 角色。当我拥有它时:

->add('roles', 'choice', array('label' => 'Rol', 'required' => true, 
                           'choices' => array( 'ROLE_ADMIN' => 'ADMINISTRADOR','ROLE_SUPERADMIN' => 'SUPERADMINISTRADOR', 
                                               'ROLE_USER' => 'USUARIO'), 'multiple' => true))

而且它有效!但是他们必须离开BD,我不能放Entity字段,因为角色应该是一个数组,而不是一个对象。如何生成具有从表中获取的角色的数组?在 FosUSerbundle 中,您将添加角色吗?

谢谢....

我写是因为那个用户没有答案。我按照[官方文档的步骤] (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md) 并在 FOSUserBundle 的寄存器中添加上述行,但我想从数据库中工作。

然后我用这个来创建组。创建了两个额外的表,甚至现在加入了列表中的组或角色,但没有显示如何显示登录以注册新用户。

有人解决了吗?

【问题讨论】:

    标签: symfony fosuserbundle


    【解决方案1】:

    所以你有一个表中的角色?您可以在表单类型中注入 EntityManager 并使用它来获取选项。

            ->add('roles', 'choice', array(
                'label' => 'Rol',
                'choices' => $this->getRoles(),
                'multiple' => true,
                'required' => true,
            ))
    

    然后创建一个方法,以您需要的方式为您提供数据。

    类似:

    public function getRoles()
    {
        $roles = array();
        $er = $this->em->getRepository('AppBundle:Role');
        $results = $er->createQueryBuilder('r')
                // conditions here
                ->getQuery()
                ->getResult();
    
        // process the array?
        foreach ($results as $role) {
            $roles[$role->getId()] = $role->getLabel();
        }
    
        return $roles;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 2013-11-06
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2013-04-06
      • 2013-04-18
      相关资源
      最近更新 更多