I have build user entity with roles which have field to store the roles in the database:
/**
* @var array
* @ORM\Column(name="roles", type="json_array")
*/
private $roles = array();
public function getRoles()
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setPassword($password)
{
$this->password = $password;
return $this;
}
It is stored like a array in the database. This is build on tutorial. I realize that if it is like a array in the database it will need to be a collection in the form builder right? Here is the code:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('password')
->add('plainPassword', 'repeated', array('type' => 'password', 'required' => false))
->add('roles', 'choice', array(
'choices' => array(
'ROLE_USER' => 'ROLE_USER',
'ROLE_ADMIN' => 'ROLE_ADMIN'
),
'multiple' => true,
))
->add('isActive')
->add('mail');
}
End the twig render
{{ form_widget(edit_form.roles) }}
So the basic idea is to have a select field with all the roles to select for a user and then update the database. But for some reason when i remove
'multiple' => true
I got error like this ...
What is the best workaround here? I want not to have multiple choice but only single one.
And the last thing is that it not populate the database but i got the information and when I call before persist it saves them but not on default.
$entity->setRoles($entity->getRoles());
