title: Symfony2 + Doctrine - Filtering
body: <p>I've got a <code>OneToMany</code> relationship where one football team has many players. I want to list all football teams and display the name of the captain for each team. </p>
  
  <p>Each player entity has a foreign key (team_id) and a field 'captain' which is set to 0 or 1. I'm currently running the following query:</p>
  
  <pre><code> $teams = $this
               -&gt;getDoctrine()
               -&gt;getRepository('FootballWebsiteBundle:Team')
                -&gt;createQueryBuilder('t')
               -&gt;setFirstResult(($pageNumber * $resultPerPage) - $resultPerPage)
               -&gt;setMaxResults($resultPerPage)
               -&gt;add('where','t.deleted = 0')
               -&gt;add('orderBy', 't.name DESC')
               -&gt;getQuery()-&gt;getResult();
  </code></pre>
  
  <p>Then when I loop through each team in twig I run team.getTeamCaptain().getName() which is a filter within my Team entity:</p>
  
  <pre><code>public function getTeamCaptain() {
      $them  = $this-&gt;players-&gt;filter(function($p) {
          return $p-&gt;getCaptain() == 1;
      });
  
      return $them-&gt;first();
  }
  </code></pre>
  
  <p>Is there a better way to run this query?</p>