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

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

问题描述

Symfony版本:3.1.3
数据库:MySQL



我有users表,它有一列作为角色 (LongText-DC2Type:array)。



在我的控制器中,我创建了一个下拉框,其形式如下:

  $ user = new Users; 
$ form = $ this-> createFormBuilder($ user)
//其他一些字段
- > add('roles',ChoiceType :: class,array(
'attr'=> array(
'class'=>'form-control',
'style'=>'margin:5px 0;'),
' => array(
'Teacher'=> true,
'Student'=> true,
'Parent'=> true
),
))
//其他一些字段
- > getForm();

然后我得到用户选择的角色为bellow,(在同一控制器内)

  if($ form-> isSubmitted()&&form $> isValid()){
/ /一些其他代码
$ role = $ form ['roles'] - > getData();
//其他代码

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();
}

但是这给我以下错误,

 类型array,boolean的预期参数给定

我想我做错了,想知道把角色插入数据库的正确方法。

解决方案

这是我做了什么来摆脱问题,



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

  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获取角色

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



a nd这是formtype中角色的代码,

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

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

   - > add('roles',ChoiceType :: class,array(
'attr'=> array('class'=> 'form-control',
'style'=>'margin:5px 0;'),
'choices'=>
array

' ROLE_ADMIN'=>数组

'是'=>'ROLE_ADMIN',
),
'ROLE_TEACHER'​​=>数组

'Yes'=>'ROLE_TEACHER'​​
),
'ROLE_STUDENT'=>数组

'是'=>'ROLE_STUDENT'
) ,
'ROLE_PARENT'=>数组

'是'=>'ROLE_PARENT'
),


'multiple'=> true,
'required'=> true,


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

  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]


Symfony version: 3.1.3 Database: MySQL

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();
}

But this gives me the following error,

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,

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]

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

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

and this is the code to roles in the formtype,

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

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,
    )
)

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天全站免登陆