Symfony3 如何在数据库中存储用户角色 [英] Symfony3 how to store user roles in database

查看:24
本文介绍了Symfony3 如何在数据库中存储用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony 版本:3.1.3数据库:MySQL

Symfony version: 3.1.3 Database: MySQL

我有用户表,它有一列作为角色(LongText-DC2Type:array).

I have the users table and it has a column as roles(LongText-DC2Type:array).

在我的控制器中,我为表单创建了一个下拉框,如下所示,

In my controller I have created a dropdown box for the form as bellow,

$user = new Users;
$form = $this->createFormBuilder($user)
        // some other fields
        ->add('roles', ChoiceType::class, array(
                    'attr'  =>  array(
                            'class' => 'form-control',
                            'style' => 'margin:5px 0;'),
                    'choices'  => array(
                            'Teacher'   => true,
                            'Student'   => true,
                            'Parent'    => true
                    ),
        ) )
        // some other fields
        ->getForm();

然后我得到用户选择的角色如下,(在同一个控制器中)

and then I am getting the user selected role as bellow,(within the same controller)

if( $form->isSubmitted() && $form->isValid() ){
    // some other codes
    $role   = $form['roles']->getData();
    // some other codes

    if( $role == 0 ){
        $userRole = array ('teacher');
    }
    elseif( $role == 1 ){
        $userRole = array ('student');
    }
    elseif( $role == 2 ){
        $userRole = array ('parent');
    }

    $user->addRole($userRole);

    $em = $this->getDoctrine()->getManager();
    $em->persist($user);
    $em->flush();
}

但这给了我以下错误,

Expected argument of type "array", "boolean" given 

我认为我做错了,想知道将角色插入数据库的正确方法.

I think I am doing it the wrong way and would like to know the right way to insert roles to the Database.

推荐答案

这是我为解决这个问题所做的,

Here is what I did to get rid the issue,

在/app/config/security.yml 中定义角色如下,

Define Roles in the /app/config/security.yml as below,

role_hierarchy:
    ROLE_ADMIN:         [ROLE_ADMIN]
    ROLE_SUPER_ADMIN:   [ROLE_SUPER_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    ROLE_TEACHER:       [ROLE_TEACHER]
    ROLE_STUDENT:       [ROLE_STUDENT]
    ROLE_PARENT:        [ROLE_PARENT]

在控制器中,使用以下代码从/app/config/security.yml 获取角色

in the Controller, get the roles from the /app/config/security.yml using the following code

$roles = $this->getParameter('security.role_hierarchy.roles');

这是formtype中角色的代码,

and this is the code to roles in the formtype,

$roles = $this->getParent('security.role_hierarchy.roles');

然后在formtype中,(这里是多选)

and then in the formtype, (here it is multi select)

->add('roles', ChoiceType::class, array(
    'attr'  =>  array('class' => 'form-control',
    'style' => 'margin:5px 0;'),
    'choices' => 
    array
    (
        'ROLE_ADMIN' => array
        (
            'Yes' => 'ROLE_ADMIN',
        ),
        'ROLE_TEACHER' => array
        (
            'Yes' => 'ROLE_TEACHER'
        ),
        'ROLE_STUDENT' => array
        (
            'Yes' => 'ROLE_STUDENT'
        ),
        'ROLE_PARENT' => array
        (
            'Yes' => 'ROLE_PARENT'
        ),
    ) 
    ,
    'multiple' => true,
    'required' => true,
    )
)

编辑用户角色必须在/app/config/security.yml 中定义如下

Edit User roles has to be defined in the /app/config/security.yml as below

role_hierarchy:
    ROLE_ADMIN:         [ROLE_ADMIN]
    ROLE_SUPER_ADMIN:   [ROLE_SUPER_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    ROLE_TEACHER:       [ROLE_TEACHER]
    ROLE_STUDENT:       [ROLE_STUDENT]
    ROLE_PARENT:        [ROLE_PARENT]

这篇关于Symfony3 如何在数据库中存储用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆