title: symfony2 - Many to many relation / Form
body: <p>I'm stuck with a Symfony2 problem involving a Many-To-Many relation : I've got a User entity, and a Project entity, linked with a Many-To-Many relation :</p>
  
  <p>My User entity :</p>
  
  <pre><code>class User implements UserInterface
  {
  
  [...]
  
  /**
   * @ORM\ManyToMany(targetEntity="Project", inversedBy="users", cascade={"persist"})
   * @ORM\JoinTable(name="user_project",
   *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
   *     inverseJoinColumns={@ORM\JoinColumn(name="project_id", referencedColumnName="id")}
   * )
   *
   * @var ArrayCollection $projects
   */
  protected $projects;
  </code></pre>
  
  <p>And my Project entity :</p>
  
  <pre><code>class Project
  {
  
   [...]
  
   /**
   * @ORM\ManyToMany(targetEntity="User", mappedBy="projects")
   */
  protected $users;
  </code></pre>
  
  <p>Symfony2 created the "link" table (called user_project), and then I inserted some rows. For example I have 2 Users (id = 1 / id = 2) and 2 Projects (id = 1 / id = 2).</p>
  
  <p>In my table user_project I inserted the following rows :</p>
  
  <ol>
  <li>1 (user_id) - 1 (project_id)</li>
  <li>1 (user_id) - 2 (project_id)</li>
  <li>2 (user_id) - 1 (project_id)</li>
  </ol>
  
  <p>So that User 1 is assigned to both projects 1 and 2, and User 2 is assigned to project 1 only.</p>
  
  <p>My problem is that I don't know how to select Projects for a specified User. For example, I want to display (in my form) a select box with the projects assigned to my user. (For User 2, I only want to have Project 1. in my select box)</p>
  
  <p>My form :</p>
  
  <pre><code>$builder-&gt;add('project', 'entity', array(
          'class' =&gt; 'WebaccessBugtrackerBundle:Project',
          'property' =&gt; 'name',
          'query_builder' =&gt; function($er) use ($userManager) {
  
              $qb = $er-&gt;createQueryBuilder('project')
              -&gt;orderBy('project.name', 'ASC');
  
              $qb-&gt; ????
  
  
              }
              return $qb;
          },
          'property' =&gt; 'name'));
  </code></pre>
  
  <p>I hope you understand what I'm saying and that you can help me :) </p>
  
  <p>Thanks !</p>