I'm trying to group my entity by a field (year) and do a count of it.
Code:
public function countYear()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('b.year, COUNT(b.id)')
->from('\My\Entity\Album', 'b')
->where('b.year IS NOT NULL')
->addOrderBy('sclr1', 'DESC')
->addGroupBy('b.year');
$query = $qb->getQuery();
die($query->getSQL());
$result = $query->execute();
//die(print_r($result));
return $result;
}
I can't seem to say COUNT(b.id) AS count
as it gives an error, and
I do not know what to use as the addOrderby(???, 'DESC')
value?
$qb->select('b.year, COUNT(b.id) AS mycount')->from('\My\Entity\Album', 'b')->where('b.year IS NOT NULL')->orderBy('mycount', 'DESC')->groupBy('b.year');
Thanks for pointing out the keyword issue. – Extol