I'm trying to get top 3 users who has featured property = true. Here's my EntityRepository:
public function getFeaturedCleaners()
{
$baseQuery = $this->_baseCleanerQuery();
return $baseQuery
->where($baseQuery->expr()->eq('c.featured', $baseQuery->expr()->literal(true)))
->andWhere($baseQuery->expr()->isNotNull('c.user'))
->orderBy('ratingTotal', 'DESC')
->setMaxResults(3)
->getQuery()->getArrayResult();
}
private function _baseCleanerQuery()
{
return $this->createQueryBuilder('c')
->select([
'c as user',
'COALESCE(AVG(rv.speed), 0) as ratingSpeed',
'COALESCE(AVG(rv.quality), 0) as ratingQuality',
'COALESCE(AVG(rv.responsibility), 0) as ratingResponsibility',
'COALESCE((AVG(rv.speed) + AVG(rv.quality) + AVG(rv.responsibility)) / 3, 0) as ratingTotal',
])
->leftJoin('c.reviews', 'rv');
}
But when there's no featured users, I get one row with null fields, instead of empty array:
array(1) { [0]=> array(5) { ["user"]=> NULL ["ratingSpeed"]=> string(6) "0.0000" ["ratingQuality"]=> string(6) "0.0000" ["ratingResponsibility"]=> string(6) "0.0000" ["ratingTotal"]=> string(10) "0.00000000" } }
Why is it happening? How can I fix it?