Symfony3 how to store user roles in database

后端 未结 3 1627
余生分开走
余生分开走 2020-12-12 07:01

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

3条回答
  •  清歌不尽
    2020-12-12 07:02

    EDIT #2

    I looked at my own code, and I gave you the wrong information. Change it to the following. Notice the way you get the role from the form is incorrect, use the below solution. I'm fairly certain this will work for you.

    ->add('roles', ChoiceType::class, array(
            'attr'  =>  array(
                    'class' => 'form-control',
                    'style' => 'margin:5px 0;'),
            'choices'  => array(
                    'Teacher'   => 0,
                    'Student'   => 1,
                    'Parent'    => 2,
            ),
    ))
    
    
    if( $form->isSubmitted() && $form->isValid() ){
        // some other codes
        $role   = $form->get('roles')->getData();
        ...
    

    @dragoste made a correct statement in that you should have first tried some troubleshooting before posting the question. Also you can search online for answers as well. There are a lot of Symfony examples available.

提交回复
热议问题