-3

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:

enter image description here

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:

enter image description here

Magic Fox
  • 1
  • 1

1 Answers1

0

It's works for me:

 public function getTopFiveLikedWorks(int $userId)
 {
     return $this->createQueryBuilder('w')
         ->select('w.title, w.id, COUNT(wl.work) AS like_count')
         ->innerJoin(WorkLikes::class, 'wl', 'with', 'wl.work = w.id')
         ->andWhere('w.user = :userId') 
         ->setParameter('userId', $userId)
         ->groupBy('w.title')
         ->orderBy('w.id', 'DESC') 
         ->orderBy('like_count', 'DESC')          
         ->getQuery()
         ->getResult();     
 }
Magic Fox
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 26 '22 at 12:13