title: Adding a Having Clause to Doctrine Statement
body: <p>I am new to Doctrine and I am trying to figure out how to add a having clause on my statement.  Basically I want to be able to filter down on items returned based on how many attributes the user selects.  The code is as follows:</p>
  
  <pre><code>// create query builder
      $qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
  
      $qb-&gt;select('p')
          -&gt;from($this-&gt;_entityName, 'p')
          -&gt;leftJoin('p.options', 'o')
          -&gt;where('p.active = :active')
          -&gt;setParameter('active', 1);
  
      // add filters
       $qb-&gt;leftJoin('o.attributes', 'a');
                  $ands = array();
  
                  foreach ($value as $id =&gt; $values)
                  { echo count($values);
                      $ands[] = $qb-&gt;expr()-&gt;andX(
                          $qb-&gt;expr()-&gt;eq('a.attribute_id', intval($id)),
                          $qb-&gt;expr()-&gt;in('a.attribute_value_id', array_map('intval', $values))
                          $qb-&gt;having('COUNT(*)=3) // THIS DOESN'T WORK
                          //$qb-&gt;expr()-&gt;having('COUNT(*)=3) // THIS DOESN'T WORK EITHER
                      );
                  }
  
                  $where = $qb-&gt;expr()-&gt;andX();
  
  
                  foreach ($ands as $and)
                  {
                      $where-&gt;add($and);
                  }
                  $qb-&gt;andWhere($where);
                  $result = $qb-&gt;getQuery()-&gt;getResult();
                  return $result;
  </code></pre>
  
  <p>When I try to execute the statement with the having() clause I get this error: 
  Expression of type 'Doctrine\ORM\QueryBuilder' not allowed in this context.</p>
  
  <p>Without the having() clause everything works perfectly.</p>
  
  <p>I have no idea how to solve this.</p>