title: Symfony2 form query_builder with parameter
body: <p>I have this error :</p>
  
  <p><em>"Catchable Fatal Error: Argument 1 passed to Intranet\RhBundle\Form\AvatarFormType::Intranet\RhBundle\Form{closure}() must be an instance of Intranet\UserBundle\Entity\ImageRepository, instance of Doctrine\ORM\EntityRepository given, called in C:\wamp\www\projet\vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader.php on line 56 and defined in C:\wamp\www\projet\src\Intranet\RhBundle\Form\AvatarFormType.php line 24"</em></p>
  
  <p>When I began to search, I found a common error on the method in the repository.
  But maybe it's okay...</p>
  
  <p>This is my ImageRepository :</p>
  
  <pre><code>public function getImageUser(User $user)
  {
      $qb = $this-&gt;createQueryBuilder('i')
             -&gt;where('i.user = :user ')
             -&gt;setParameter('user', $user);
  
  // Et on retourne simplement le QueryBuilder, et non la Query
  return $qb;
  </code></pre>
  
  <p>}</p>
  
  <p>This is my <code>AvatarFormType</code>:</p>
  
  <pre><code>/**
   * @param FormBuilderInterface $builder
   * @param array $options
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      //die(var_dump($options['data']));
      $builder
         -&gt;add('avatar', 'entity', array(
              'class' =&gt; 'IntranetUserBundle:Image',
              'property' =&gt; 'alt',
             'query_builder' =&gt; function(ImageRepository  $r) use($options) {
                  return $r-&gt;getImageUser($options['user']);}
              )
          );
  }
  </code></pre>
  
  <p>The relation :</p>
  
  <pre><code>/**
   * @ORM\OneToOne(targetEntity="Intranet\UserBundle\Entity\Image", cascade={"persist", "remove"})
   * @Assert\Valid()
   */
  private $avatar;
  </code></pre>
  
  <p>And this is my controller :</p>
  
  <pre><code>public function imagesDeAction(Request $request, User $user) {
      $form = $this-&gt;createForm(new AvatarFormType(), $user, array('user' =&gt; $user));
      $images = $this-&gt;getDoctrine()
      -&gt;getRepository('IntranetUserBundle:Image')
      -&gt;findByUser($user);
      if ($request-&gt;getMethod() == 'POST') {
          $form-&gt;handleRequest($request);
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $user-&gt;setAvatar($request-&gt;avatar);;
              $em-&gt;persist($user);
              $em-&gt;flush();
          }
  
      }
      $avatar = $user-&gt;getAvatar();
     return $this-&gt;render('IntranetRhBundle:Image:imagesDe.html.twig',array('user' =&gt; $user,'images' =&gt; $images, 'form' =&gt; $form-&gt;createView()));
  }
  </code></pre>
  
  <p>Users have some pictures in there private directory and I want to choose an avatar.
  There is already a ManyToMany relation between User and Image and a OneToOne (as I noticed) between User and Image.</p>
  
  <p>I'm trying to build a select list with only the pcitures of a specific user with the parameter. None of the solution I found is efficient to solve this error.</p>
  
  <p>I'm not sure I have to call my function with use($options) and $options['data'] but with a var_dump I saw my User on $options['data'].</p>
  
  <p>EDIT :</p>
  
  <p>I bring a little precision :
  The ImageReposiroty seems to be not found although the use is ok. I don't have the error message "class not found". 
  But if I put EntityReposirtoy the bug disappears and I have this symfony error message : </p>
  
  <blockquote>
    <p>Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given</p>
  </blockquote>
  
  <p>But I know I have to call ImageRepository and not EntityRepository...</p>