title: Doctrine 2: Count Items by Type in Select
body: <p>Does anyone know how to retrieve a count based on a condition in a Doctrine 2 QueryBuilder select() statement?</p>
  
  <p>Here's what I've tried so far...</p>
  
  <p>My first attempt was to try count() with eq(). The error I receive says "Expected close parentheses, got equals."</p>
  
  <pre><code>$qb-&gt;select($qb-&gt;expr()-&gt;count($qb-&gt;expr()-&gt;eq('t.id', '1')))
  </code></pre>
  
  <p>Next I tried count() with having(). The error I receive says "Maximum function nesting level reached."</p>
  
  <pre><code>$qb-&gt;select($qb-&gt;expr()-&gt;count($qb-&gt;having('t.id', '1')))
  </code></pre>
  
  <p>Then I tried count() with where() and eq(). Again I got "Maximum function nesting level reached."</p>
  
  <pre><code>$qb-&gt;select($qb-&gt;expr()-&gt;count($qb-&gt;where($qb-&gt;expr()-&gt;eq('t.id', '1'))))
  </code></pre>
  
  <p>Then I tried these variations using in(). They both give the syntax error "Expected FROM, got '('</p>
  
  <pre><code>$qb-&gt;select($qb-&gt;expr()-&gt;count($qb-&gt;expr()-&gt;in('t.id', array(1))))
  $qb-&gt;select($qb-&gt;expr()-&gt;count($qb-&gt;expr()-&gt;in('t.id', 1)))
  </code></pre>
  
  <p>For the in() examples, I also tried passing in the value as a variable and via setParameter(), with the same result.</p>
  
  <p>Here is the MySQL equivalent of what I'm trying to code in QueryBuilder:</p>
  
  <pre><code>SELECT
      SUM(IF(type.id = 1, 1, 0)) AS 'fords',
      SUM(IF(type.id = 2, 1, 0)) AS 'hondas'
  FROM item
  JOIN type ON item.type_id = type.id
  </code></pre>