I have table Question. Question belongs to questioncategory:
Question:
----------------------------
| id | questioncategory_id |
----------------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 1 |
----------------------------
I want to build a query with QueryBuilder or DQL, to retrieve random row for each questioncategory. Questioncategories are stored in array ($ids).
Current query (returns first question for each category):
$query = $repository->createQueryBuilder('p')
->where('p.questioncategory IN (:questionCat)')
->groupBy('p.questioncategory')
->setParameter('questionCat', $ids)
->getQuery();
Any ideas? I know how to retrieve random row for all questions, it's described here. I have to retrieve random row for each category, so I have to somehow count number of questions for each category and then select random row for that category.