title: Count rows in Doctrine
body: <p>I want count unique $hotel in SearchResult.   I new variable to pass in view.</p>
  
  <p>Class:</p>
  
  <pre><code> * 
       *
       * @ORM\Entity(repositoryClass="Uts\HotelBundle\Repository\SearchResult")
       * @ORM\Table(name="search_result")
       */
      class SearchResult
      {
          ------
          /**
           * @ORM\ManyToOne(targetEntity="SearchRequest")
           * @var
           */
          private $request;
  
          /**
           * @ORM\ManyToOne(targetEntity="Hotel")
           * @var Hotel
           */
          private $hotel;
      ---
      }
  </code></pre>
  
  <p>Pass variable to view:</p>
  
  <pre><code>   $query2 = $repository-&gt;hotelsCount($searchId);
               //  var_dump($query2);
                 $templateVars['count']=$query2;
              }
              return $this-&gt;render('UtsHotelBundle:Default:results.html.twig', $templateVars)
  </code></pre>
  
  <p>Function hotelsCount:</p>
  
  <pre><code>  public function hotelsCount($requestId){
  
       $qb = $this-&gt;createQueryBuilder('self');
      $qb-&gt;select('count DISTINCT(p.hotel)');
      $qb-&gt;from('HotelBundle:SearchResult','p')
          -&gt;where('p.request_id = :id')
          -&gt;setParameter('id',$requestId);
      $count = $qb-&gt;getQuery()-&gt;getSingleScalarResult();
      }
  </code></pre>
  
  <p>Table searchResult:<a href="https://i.stack.imgur.com/nNGJH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nNGJH.png" alt="enter image description here"></a></p>