Symfony3 how to store user roles in a database

Symfony Version: 3.1.3 Database: MySQL

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

In my controller, I created a drop-down list for the form below,

$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 get the user selected role below (inside one 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 that I am doing it wrong and would like to know the correct way to insert roles into the database.

+1
source share
3 answers

Here is what I did to get rid of the problem,

Define the roles in the /app/config/security.yml file as shown 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,

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

,

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

( 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 /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]
+2

, .

'choices' => array(
    'Teacher' => ['teacher'],
    'Student' => ['student'],
    'Parent'  => ['parent'],
)
0

№ 2

I looked at my own code and I gave you the wrong information. Change it to the following. Please note that the way you get the role from the form is incorrect, use the solution below. I am sure 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 the correct statement that before posting a question, you should first try to troubleshoot. You can also search online for answers. There are many examples of symfony.

0
source

Source: https://habr.com/ru/post/1614467/


All Articles