I'd like to order only the result of the previous main query.
I want the top 10 most viewed articles then ordered it by "addedAt date" DESC.
I'm aware of functions such as orderBy or even addOrderBy but it's not what I need here or I'm not implementing it correctly.
Example to get the top 2 on 5 articles :
- name : a || views : 100 || addedAt : 10/04/2017
- name : b || views : 10 || addedAt : 17/04/2017
- name : c || views : 50 || addedAt : 15/04/2017
- name : d || views : 25 || addedAt : 12/04/2017
- name : e || views : 200 || addedAt : 05/04/2017
I extract first the most views :
- Article e
- Article a
And then orderedIt by addedAt date DESC (most recent) :
- Article a
- Article e
My query :
public function findMostViewed($limit=10, $asArray=false)
{
$qb = $this->createQueryBuilder('n')
->orderBy('n.nbViews', 'DESC')
//->addOrderBy('n.addedAt', 'DESC')
->getQuery()
->setMaxResults($limit);
return ($asArray) ? $qb->getResult(Query::HYDRATE_ARRAY) : $qb->getResult();
}
If I knew how to do a preSelectQuery it would have probably solve the problem ...
I thought to an SQL query :
SELECT ...
FROM ...
WHERE id IN(SELECT id FROM News ORDER BY nbViews DESC LIMIT 10)
ORDER BY addedAt DESC