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.
$roleand$userRole. That should tell you more. - Jakub Matczak$user->addRole($userRole);? - Alvin Bunk$user->addRole($userRole);code - mapmalith