Doctrine2, get the essence of orders by the amount of property of many

I have two objects

  • Article
  • User

This article relates to a user named 'popularByUsers'

Now I would like to get the order of articles by the number of liked, but:

  • I do not want to have the 'numberOfLikes' property because it is too big a problem to update it.
  • I have too many articles (100k +) to be realistic for "sorting" in the PHP side (and the fact that we reach the limit of sorting is the reason why I ask this question)
  • I can live without getting the amount I liked in the return values ​​(since the serializer will later moisten it)

what i have now:

$builder = $this->createQueryBuilder('a');
    ->select('COUNT(u) AS nbrLikes')
    ->leftJoin('a.likedByUsers', 'u') 
    ->orderBy('nbrLikes', 'DESC')
    ->groupBy('a.id')
    ->getQuery()
    ->getResult()
;

( 0 ),

->select('a, COUNT(u) AS HIDDEN nbrLikes')

, a GROUP BY

?

+4
2

"" , . ,

 $builder = $this->createQueryBuilder('a')
        ->select('COUNT(u) AS HIDDEN nbrLikes', 'a.id')
        ->leftJoin('a.likedByUsers', 'u') 
        ->orderBy('nbrLikes', 'DESC')
        ->groupBy('a.id')
        ->getQuery()
        ->getResult();

, ,


 $builder = $this->createQueryBuilder('a')
        ->select('COUNT(u) AS HIDDEN nbrLikes', 'a')
        ->leftJoin('a.likedByUsers', 'u') 
        ->orderBy('nbrLikes', 'DESC')
        ->groupBy('a')
        ->getQuery()
        ->getResult();
+6

2 :

->addSelect

( ) queryBuilder, .

:

    $query = $this->getEntityManager()->createQueryBuilder()
        ->select('article, nbrLikes, ...')
        ->from('BundleName:ClassName', 'article')
        ->leftJoin('article.likedByUsers', 'nbrLikes')

. , . , .

0

All Articles