Skip to content Skip to sidebar Skip to footer

Symfony Doctrine Orderby And Group By (distinct)

I am trying to get the latest booked courses (unique). I have tried the following with the Doctrine querybuilder: $repository = $this->getDoctrine()->getRepository('AppBundle

Solution 1:

You could try this way:

$query = $repository->createQueryBuilder('b')
->select('(b.course) AS course')
->leftJoin('AppBundle:Booking', 'b2', 'WITH', 'b.course = b2.course AND b.id < b2.id')
->where('b2.id IS NULL')
->orderBy('b.id', 'DESC')
->setMaxResults(5)
->getQuery();

You'll get only the bookings with greatest id for every course, so no need for grouping by course.

Post a Comment for "Symfony Doctrine Orderby And Group By (distinct)"