I have some problem with my query, when try get top five works that get the quantity of likes. That my code, please help me improve his and decide this problem, because I'm get next result:
My code:
public function getTopFiveLikedWorks(int $userId)
{
return $this->createQueryBuilder('wl')
->select('w.title, COUNT(wl.id) as like_count')
->innerJoin(Work::class, 'w', 'with', 'w.id = wl.work')
->innerJoin(User::class, 'u', 'with', 'u.id = wl.user')
->andWhere('wl.user = :userId')
->setParameter('userId', $userId)
->orderBy('like_count', 'DESC')
->getQuery()
->getResult();
}
I'm rewrite my code and get that result:
public function getTopFiveLikedWorks(int $userId)
{
return $this->createQueryBuilder('w')
->select('w.title, COUNT(wl.work) as like_count')
->innerJoin(WorkLikes::class, 'wl', 'with', 'wl.user = w.user')
->andWhere('w.user = :userId')
->setParameter('userId', $userId)
->groupBy('w.title')
//->orderBy('w.id', 'DESC')
->orderBy('like_count', 'DESC')
->getQuery()
->getResult();
}
Screenshot: