So i've seen many subjects about my issue (greatest-n-per-group), but it seems like i just can't make it work with the queryBuilder! (for example : post)
I'm running symfony 4.2.2
Entities
------------- -------------
News Taxonomy
------------- -------------
id id
title name
category (ManyToOne Taxonomy) value
status (ManyToOne Taxonomy)
publication_date
Category can take the following values: tech / sports / games .. and so on.
Status can take the following values: draft / scheduled / published.
What i want
In my NewsRepository using the queryBuilder, i would like to return the latest published news of each category.
What i ended up doing
return $this->createQueryBuilder('a')
->select('n news', 'n.publicationDate pubDate')
->leftJoin('n.category', 'c')
->addSelect('c.value category')
->leftJoin('n.status', 's')
->addSelect('s.value status')
->andWhere('s.value = :st')
->setParameter('st', Taxonomy::WORKFLOW_PUBLISHED)
->orderBy('n.category, n.datePublication', 'DESC')
->getQuery()
->getResult();
Taxonomy::WORKFLOW_PUBLISHED being a class constant storing : 'published'
Then in a service i'm just iterating over the results returned by the previous method and extracting the latest news per category.
I dislike this temp solution, so if anyone could help me with the queryBuilder that would be great !
+ Additional information
I tried with a groupby on categories, but it wasn't returning the latest values.
If possible, i'm looking for good performance as well.