0
votes

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.

3
Have you done any debugging? Check values of $role and $userRole. That should tell you more. - Jakub Matczak
I have not done this debug will try out this. - mapmalith
The error you show, is that for the line $user->addRole($userRole);? - Alvin Bunk
I think a problem is in your choicetype field. You have array witch 3 keys with the same boolean value "true". "Teacher" => true, "Student" => true, "'Parent" => true'' Replace "true" values for "0","1","2" and i think this solved your problem - ciurciurek
@AlvinBunk yes the error is for the $user->addRole($userRole); code - mapmalith

3 Answers

1
votes

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]
0
votes

Your roles values might be directly as keys in your array choices if Users can only have one Role.

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

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.