title: Symfony2 & Doctrine2: Custom Entity Repository Query to Retrieve Single Result if Joined Table has No Associated Rows
body: <p>I have two entities/tables, one for Counties and one for Cities. A particular county has a OneToMany relationship to cities and I am trying to make a custom query in the Entity Repository to query a County based on it's ID and return that County and the Cities corresponding to it.</p>
  
  <p>My query currently seems to work great if the county has cities assigned, but if it does not have any cities yet, Doctrine gives me a "Unable to find County entity. " Exception. </p>
  
  <p>I believe there is a logical error in my query, but I am having a hard time re-writing it to return solely the County by ID if no cities are associated with it.</p>
  
  <p>My Query:</p>
  
  <pre><code>class CountyRepository extends EntityRepository
  {
    public function findOneByIdJoinedToCities($id)
    {
      $qb = $this-&gt;createQueryBuilder('c')
                 -&gt;addSelect('p')
                 -&gt;join('c.cities', 'p')
                 -&gt;where('p.county = :id')
                 -&gt;setParameter('id', $id)
                 ;
  
      $query = $qb-&gt;getQuery();
  
      try {
          return $query-&gt;getSingleResult();
      } catch (\Doctrine\ORM\NoResultException $e){
          return null;
      }
    }
  }
  </code></pre>
  
  <p>How could I change the above code to still give back a single result for County if no Cities have been assigned to it yet?</p>
  
  <p>Thanks for the help!</p>