title: Multiple COUNT query on the same Entity type using Symfony 2.1 and Doctrine 2
body: <p>For a project I need to give a load of diverse data in JSON format. All the information will be used on the same page so a single call would result in the least overhead. The information all concerns the same database object and is all necessary on the page. It basically is a collection of counts of the amount of objects that are of one or more of a certain type (the types are all booleans) and we require to know a lot of different variations of this. I used the code below but my co-worker believes the way I put it in the JSON list is a bit clunky and the code could has a greater performance. How could I improve this code?</p>
  
  <pre><code>public function getContactsStatisticsAction()
  {
      $response = new Response();
      $json = array();
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
      $cr = $em-&gt;getRepository('BlaCoreBundle:Company');
  
      $json['numberOfCompanies'] = $cr-&gt;numberOfCompanies();
      $json['numberOfAccounts'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; true));
      $json['numberOfCompetitors'] = $cr-&gt;numberOfCompanies(array("typeCompetitor" =&gt; true));
      $json['numberOfSuppliers'] = $cr-&gt;numberOfCompanies(array("typeSupplier" =&gt; true));
      $json['numberOfOthers'] = $cr-&gt;numberOfCompanies(array("typeOther" =&gt; true));
      $json['numberOfUnassigned'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; false, "typeCompetitor" =&gt; false,"typeSupplier" =&gt; false,"typeOther" =&gt; false));
  
      $json['numberOfJustAccounts'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; true, "typeCompetitor" =&gt; false, "typeSupplier" =&gt; false));
      $json['numberOfJustCompetitors'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; false, "typeCompetitor" =&gt; false, "typeSupplier" =&gt; false));
      $json['numberOfJustSuppliers'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; false, "typeCompetitor" =&gt; false, "typeSupplier" =&gt; false));
  
      $json['numberOfCompetitorAndAccounts'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; true, "typeCompetitor" =&gt; true, "typeSupplier" =&gt; false));
      $json['numberOfCompetitorAndSuppliers'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; false, "typeCompetitor" =&gt; true, "typeSupplier" =&gt; true));
      $json['numberOfSupplierAndAccounts'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; true, "typeCompetitor" =&gt; false, "typeSupplier" =&gt; true));
      $json['numberOfCompaniesAndAccountsAndSuppliers'] = $cr-&gt;numberOfCompanies(array("typeAccount" =&gt; true, "typeCompetitor" =&gt; true, "typeSupplier" =&gt; true));
  
      $response-&gt;setContent(json_encode($json));
      return $response;
  }
  
  
  public function numberOfCompanies($filters = array())
  {
      $qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
      $qb-&gt;select('count(c.id)');
      $qb-&gt;from('BlaCoreBundle:Company', 'c');
      $sizeFilters = count ($filters);
      $keys = array_keys($filters);
      if($sizeFilters &gt;= 1){
          $qb-&gt;where('c.' . $keys[0] . ' = ' . (int) $filters[$keys[0]]);
      }
      for($i = 1; $i &lt; $sizeFilters; $i++){
          $qb-&gt;andWhere('c.' . $keys[$i] . ' = ' . (int) $filters[$keys[$i]]);
      }
      return $qb-&gt;getQuery()-&gt;getSingleScalarResult();
  }
  </code></pre>