title: Symfony form query_buider and entity repository
body: <p>I'm trying to create a form with data in collection type depending on the user being logged. I'm following <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html" rel="noreferrer">this chapter of the Symfony cookbook</a>.</p>
  
  <p>Everything works fine when the <code>query_builder</code> option is a closure where I get my data from DQL. As the data need to be fetched from different location in code, I would prefer to define the query in the Repository class.</p>
  
  <p>Here is the function in my repository :</p>
  
  <pre><code>public function findOwnedBy($user) {
      $query = $this-&gt;getEntityManager()-&gt;createQuery("SELECT l FROM MyBundle:Article a JOIN a.owndBy u WHERE u.id = :userId");
      $query-&gt;setParameters(array("userId"=&gt;$user-&gt;getId()));
      return $query-&gt;getResult();
  }
  </code></pre>
  
  <p>This function works when called in a Controller and return an array of Article. Here is a snippet of the symfony doc :</p>
  
  <pre><code>$formOptions = array(
                      'class' =&gt; 'Acme\DemoBundle\Entity\User',
                      'multiple' =&gt; false,
                      'expanded' =&gt; false,
                      'property' =&gt; 'fullName',
                      'query_builder' =&gt; function(EntityRepository $er) use ($user) {
                          // build a custom query, or call a method on your repository (even better!)
                      },
                  );
  </code></pre>
  
  <p>When I put a call to my Repository function in the query_builder, I get an error : <code>Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given</code>, which I can understand because my Repository returns an array of Entity, not a QueryBuilder.</p>
  
  <p>I don't want to duplicate code and create a new QueryBuilder in the Form. What is the best practice to use the query from the Repository ? I was thinking of having two function in the repository, one returning an array and the other returning the QueryBuilder, but the comment in Symfony doc "<em>or call a method on your repository (even better!)</em>" let me think there's better way for this case.</p>