title: Catchable Fatal Error: Argument 1 passed to ? Symfony2
body: <p>I am stuck and frustrated with the bellow error message:</p>
  
  <blockquote>
    <p>Catchable Fatal Error: Argument 1 passed to Medicine\UserBundle\Entity\User
        ::setUsertype() must be an instance of Medicine\UserBundle\Entity\Usertype, 
        instance of Doctrine\Common\Collections\ArrayCollection given, called in
        /opt/lampp/htdocs/drugs/vendor/symfony/src/Symfony/Component/Form/Util
        /PropertyPath.php on line 347 and defined in /opt/lampp/htdocs/drugs/src/
        Medicine/UserBundle/Entity/User.php line 224</p>
  </blockquote>
  
  <p>What I think this error is due to use of manytoone field in my entity, I even tried with keeping onetomany in another entity.</p>
  
  <p>I have a user entity and a usertype entity, the usertype_id is a manytoone field in user table. here is the code for both the entities:-</p>
  
  <p><strong>User</strong></p>
  
  <pre><code>namespace Medicine\UserBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
  * @ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UserRepository")
  * @ORM\Table(name="user")
  * @ORM\HasLifecycleCallbacks()
  */
  
  class User
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @ORM\HasLifecycleCallbacks()
   */
  protected $id;
  
  /**
   * @ORM\Column(type="string")
   */
   protected $username;
  
   /**
   * @ORM\ManyToOne(targetEntity="Usertype", inversedBy="users")
   * @ORM\JoinColumn(name="usertype_id", referencedColumnName="id")
   */
   protected $usertype;
  
  /**
   * @ORM\Column(type="string")
   */
   protected $image;
  
  /**
   * Set usertype
   *
   * @param Medicine\UserBundle\Entity\Usertype $usertype
   */
  public function setUsertype(\Medicine\UserBundle\Entity\Usertype $usertype)
  {
      $this-&gt;usertype = $usertype;
  }
  
  /**
   * Get usertype
   *
   * @return Medicine\UserBundle\Entity\Usertype 
   */
  public function getUsertype()
  {
      return $this-&gt;usertype;
  }
  }
  </code></pre>
  
  <p>I am just showing the concerned code, i have all the getter and setter methods for the above code.</p>
  
  <p><strong>UserType</strong></p>
  
  <pre><code>namespace Medicine\UserBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
  * @ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UsertypeRepository")
  * @ORM\Table(name="usertype")
  * @ORM\HasLifecycleCallbacks()
  */
  
  class Usertype
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @ORM\HasLifecycleCallbacks()
   */
  protected $id;
  
  /**
   * @ORM\Column(type="string")
   */
  protected $name;
  
  /**
  * @ORM\OneToMany(targetEntity="User", mappedBy="usertype")
  */
  protected $users;
  
  public function __construct()
  {
      $this-&gt;users = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Add users
   *
   * @param Medicine\UserBundle\Entity\User $users
  */
  public function addUser(\Medicine\UserBundle\Entity\User $users)
  {
      $this-&gt;users[] = $users;
  }
  
  /**
   * Get users
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getUsers()
  {
      return $this-&gt;users;
  }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <p>This Executes when a user wants to login. He will fill in the username password and a UserType:</p>
  
  <pre><code>public function indexAction()
  {
  $entity = new User();
      $form = $this-&gt;createForm(new LoginForm(), $entity);
      $request = $this-&gt;getRequest();
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;bindRequest($request);
          if ($form-&gt;isValid()) {
          echo "&lt;pre&gt;"; print_r($entity-&gt;getUsertype()); exit;
          $em = $this-&gt;getDoctrine()
                -&gt;getEntityManager();
          $em-&gt;persist($entity);
          $userrepository = $em-&gt;getRepository('MedicineUserBundle:User');
          echo "&lt;pre&gt;"; print_r($entity-&gt;getUsertype()); exit;
          $all = $userrepository-&gt;findOneBy(array('login' =&gt; $entity-&gt;getLogin(), 'password' =&gt; $entity-&gt;getPassword()));
          if($all)
          {
              return $this-&gt;redirect($this-&gt;generateUrl('MedicineUserBundle_login'));
          }
          }
       }
  
       return $this-&gt;render('MedicineUserBundle:User:loginpage.html.twig',array(
          'form' =&gt; $form-&gt;createView()
      ));
  }
  </code></pre>
  
  <p><strong>Login Form</strong></p>
  
  <pre><code> public function buildForm(FormBuilder $builder, array $options)
  {
      $builder
      -&gt;add('login', 'text', array('label'  =&gt; 'Username',))
          -&gt;add('password','password')
      -&gt;add('usertype', 'entity', array('class' =&gt; 'MedicineUserBundle:Usertype', 'property'=&gt;'name', 'multiple'  =&gt; true, ))
  
      ;
  }
  </code></pre>