title: Errors in Setting Roles Symfony 2 Security
body: <p>I am creating a user form in which Roles are selected via form dropdown.My form and entity looks like this</p>
  
  <pre><code>//entity
  
  private $roles;
  public function getRoles()
  {
  
      return $this-&gt;roles;
  }
  
  //form
  
  class RoleType extends AbstractType
  {
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'choices' =&gt; array(
              'ROLE_USER' =&gt; 'ROLE_USER',
              'ROLE_ADMIN' =&gt; 'ROLE_ADMIN',
          )
      ));
  }
  
  public function getParent()
  {
      return 'choice';
  }
  
  public function getName()
  {
      return 'role';
  }
  
  
  class RegistrationType extends AbstractType
  {
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
  $builder-&gt;add('username')
          -&gt;add('password', 'repeated', array(
              'type' =&gt; 'password',
              'invalid_message' =&gt; 'The password fields must match.',
              'options' =&gt; array('attr' =&gt; array('class' =&gt; 'password-field')),
              'required' =&gt; true,
              'first_options'  =&gt; array('label' =&gt; 'Password'),
              'second_options' =&gt; array('label' =&gt; 'Repeat Password'),
          ))
          //-&gt;add('terms', 'checkbox', array(
             // 'property_path' =&gt; 'termsAccepted',
         // ))
          -&gt;add('firstname')
          -&gt;add('lastname')
          -&gt;remove('photo')
          -&gt;add('email', 'email')
          -&gt;remove('status')
          -&gt;remove('createdBy')
          -&gt;remove('isActive')
          -&gt;remove('dateCreated')
          -&gt;remove('updatedBy')
          -&gt;remove('dateUpdated')
          //-&gt;remove('roles', 'choice', array(
             // 'choices' =&gt; array('ROLE_USER' =&gt; 'ROLE_USER', 'ROLE_ADMIN' =&gt; 'ROLE_ADMIN'),
          //))
          -&gt;add('roles', new RoleType(), array(
              'placeholder' =&gt; 'Choose Roles',
          ))
          //-&gt;add('terms', 'checkbox', array(
            //  'property_path' =&gt; 'termsAccepted',
             // 'attr' =&gt; array('class' =&gt; 'checkbox'),
         // ))
          -&gt;add('Register', 'submit')
      ;
  }
  
  public function getName()
  {
      return 'registration';
  }
  </code></pre>
  
  <p>I have no problem when creating a user where Roles are selected via form dropdown.The problem is, during login, it throws  errors that something must be in array, not in string</p>
  
  <blockquote>
    <p>Catchable Fatal Error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, string given, called in /var/www/</p>
  </blockquote>
  
  <p>I change my user entity </p>
  
  <pre><code> private $roles = array();
   public function setRoles($roles)
  {
      $this-&gt;roles = $roles;
  
      return $this;
  }
  
   public function getRoles()
   {
     return array($this-&gt;roles);
   }
  </code></pre>
  
  <p>The user can now successfully login, but it throws an error when adding another user in the form</p>
  
  <blockquote>
    <p>The value of type "array" cannot be converted to a valid array key.</p>
  </blockquote>
  
  <p>This is just a simple user bundle.I dont want to use another third party bundle like FOSUsersrBundle.Any idea how to fix this?</p>
  
  <p>update</p>
  
  <p>In the controller, I use to store this into database</p>
  
  <pre><code>public function createAction(Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      //$form = $this-&gt;createForm(new RegistrationType(), new Registration());
      $form = $this-&gt;createForm(new RegistrationType(), new Users());
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $registration = new Users();
          $registration = $form-&gt;getData();
          $registration-&gt;setDateCreated(new \DateTime());
          //$registration-&gt;setRoles('ROLE_ADMIN');
          $registration-&gt;setPhoto('http://www.gravatar.com/avatar/'.md5(trim($request-&gt;get('email'))));
          $pwd=$registration-&gt;getPassword();
          $encoder=$this-&gt;container-&gt;get('security.password_encoder');
          $pwd=$encoder-&gt;encodePassword($registration, $pwd);
          $registration-&gt;setPassword($pwd);
          $em-&gt;persist($registration);
          $em-&gt;flush();
  
          $this-&gt;addFlash('danger', 'New User successfully added!');
          return $this-&gt;redirect($this-&gt;generateUrl('voters_list'));
      } else {
          $this-&gt;addFlash('danger', 'Some errors buddy cannot proceed, please check!');
          return $this-&gt;render('DuterteBundle:Security:register.html.twig',array(
              'form' =&gt; $form-&gt;createView())
          );
      }
  </code></pre>
  
  <p>update</p>
  
  <p>My basic mapping</p>
  
  <pre><code>Project\Bundle\DuterteBundle\Entity\Users:
  type: entity
  table: users
  repositoryClass: Project\Bundle\DuterteBundle\Repository\UsersRepository
  #indexes:
      #role_id_idx:
          #columns:
              #- role_id
  id:
      id:
          type: integer
          nullable: false
          unsigned: false
          comment: ''
          id: true
          generator:
              strategy: IDENTITY
  fields:
      username:
          type: string
          nullable: false
          length: 50
          fixed: false
          comment: ''
      password:
          type: string
          nullable: false
          length: 32
          fixed: false
          comment: ''
      firstname:
          type: string
          nullable: true
          length: 50
          fixed: false
          comment: ''
      lastname:
          type: string
          nullable: true
          length: 50
          fixed: false
          comment: ''
      photo:
          type: string
          nullable: true
          length: 200
          fixed: false
          comment: ''
      email:
          type: string
          nullable: true
          length: 200
          fixed: false
          comment: ''
      status:
          type: string
          nullable: true
          length: 8
          fixed: false
          comment: ''
      roles:
          type: string
          nullable: true
          length: 100
          fixed: false
          comment: ''
  </code></pre>
  
  <p>in mysql, the users table includes the roles column where the roles are stored e.g 'ROLE_USERS',"ROLE_ADMIN' etc.Changing the getRoles function from the users entity</p>
  
  <pre><code>  public function getRoles()
    {
          return array('ROLE_USER'_)
    }
  </code></pre>
  
  <p>Will successfully logs a user, but the roles is always set to 'ROLE_USERS' even if int the database, a user's role is equivalent to 'ROLE_ADMIN'</p>