Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I do MySQL count(*) in Doctrine2?

I have the following Doctrine2 query:

$qb = $em->createQueryBuilder()
      ->select('t.tag_text, COUNT(*) as num_tags')
      ->from('CompanyWebsiteBundle:Tag2Post', 't2p')
      ->innerJoin('t2p.tags', 't')
      ->groupBy('t.tag_text')
;
$tags = $qb->getQuery()->getResult();

When run I get the following error:

[Semantical Error] line 0, col 21 near '*) as num_tags': Error: '*' is not defined. 

How would I do MySQL count(*) in Doctrine2?

like image 797
matthew Avatar asked Aug 23 '12 08:08

matthew


1 Answers

You should be able to do it just like this (building the query as a string):

$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
like image 122
Asciiom Avatar answered Oct 05 '22 04:10

Asciiom