title: Warning: in_array() expects parameter 2 to be array, object given While Saving One to Many Relationship forms
body: <p>i have one to many relationship between my user and roles. while creating a new user i have render all the roles from roles entity and render it on with create user form. after putiting user info and selecting roles. when inserting into the db it says
  <code>Warning: in_array() expects parameter 2 to be array, object given</code></p>
  
  <p>Below is my code.</p>
  
  <p><strong>User Entity</strong></p>
  
  <pre><code>    /**
       * @ORM\OneToMany(targetEntity="PNC\PermissionsBundle\Entity\Roles", mappedBy="users", cascade={"persist"})
       * @var ArrayCollection $role
       */
      private $role;
  /**
       * @return ArrayCollection A Doctrine ArrayCollection
       */
      public function getRole()
      {
          return $this-&gt;role;
      }
  
      /**
       * @param mixed $role
       */
      public function setRole(ArrayCollection $role)
      {
          foreach($role as $rol)  {
              $rol-&gt;setUsers($this);
          }
          $this-&gt;role = $role;
      }
  
      public function __construct() {
          parent::__construct();
          $this-&gt;roles = new ArrayCollection();
  
      }
  </code></pre>
  
  <p><strong>Roles Entity:</strong></p>
  
  <pre><code>  /**
       * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="role")
       * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
       */
      private $users;
  /**
       * Set Users
       *
       * @param \Doctrine\Common\Collections\ArrayCollection $users
       * @return Roles
       */
      public function setUsers(ArrayCollection $users)
      {
          $this-&gt;users = $users;
          return $this;
      }
      /**
       * Get users
       *
       * @return \Doctrine\Common\Collections\ArrayCollection
       */
      public function getUsers()
      {
          return $this-&gt;users;
      }
  </code></pre>
  
  <p><strong>and the form:</strong></p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('firstname','text',array(
              'label' =&gt; ucfirst('First Name *'),
              'attr'=&gt; array(
                  'class' =&gt; 'form-control',
                  'autocomplete' =&gt; 'off'
              )))
               -&gt;add('role', 'entity', array(
                   'class' =&gt; 'PNCPermissionsBundle:Roles',
                   'property' =&gt; 'role',
                   //'choice_list' =&gt; new SimpleChoiceList($optionArray),
                   'multiple' =&gt; true,
                   'expanded' =&gt; true,
                   'attr'=&gt; array(
                       'class' =&gt; 'minimal',
                       'style' =&gt; 'position: absolute; opacity: 0;'
                   )
               ))
  ......
          -&gt;add('save', 'submit', array(
              'attr' =&gt; array('class' =&gt; 'submit'),
              'label' =&gt; "Save"
          ))
          ;
      }
  </code></pre>
  
  <p><strong>and the controller:</strong></p>
  
  <pre><code> public function newAction(Request $request){
  
          $basePath = $request-&gt;getScheme().'://'.$request-&gt;getHost().$request-&gt;getBaseUrl();
          $user = new User();
          $form = $this-&gt;createForm(new UsersType(), $user, array(
              'action' =&gt; $this-&gt;generateUrl('pnc_permissions_user_add'),
              'method' =&gt; 'POST',
          ));
          if ($request-&gt;getMethod() == 'POST')
          {
              $form-&gt;handleRequest($request);
              if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
                  $em = $this-&gt;getDoctrine()-&gt;getManager();
                  $factory = $this-&gt;get('security.encoder_factory');
                  $encoder = $factory-&gt;getEncoder($user);
                  $password = $encoder-&gt;encodePassword( $user-&gt;getPassword() , $user-&gt;getSalt());
                  $user-&gt;setPassword($password);
                  $em-&gt;persist($user);
                  $em-&gt;flush();
                  $this-&gt;get('session')-&gt;getFlashBag()-&gt;add('notice', 'New user created successfully.');
                  return $this-&gt;redirect($this-&gt;generateUrl('pnc_permissions_user_index'));
              }
          }
          return $this-&gt;render('PNCPermissionsBundle:Users:add_user.html.twig', array(
              'userForm' =&gt;$form-&gt;createView(),
          ));
      }
  </code></pre>
  
  <p>New to symofny, unable to figure out the error. Plus i am using fos user bundle also. and on error page it says this;</p>
  
  <pre><code>Stack Trace
  in vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Model/User.php at line 142   -
              return $this;
          }
          if (!in_array($role, $this-&gt;roles, true)) {
              $this-&gt;roles[] = $role;
          }
  </code></pre>