Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine - select last 4 rows from table

I'm using Symfony/Doctrine. I'm trying to select last 4 rows from table, but im getting error.

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'SELECT c FROM DprocMainBundle:Courses c ORDER BY id DESC LIMIT 4'
);

$course = $query->getResult();

This is my query but it shows error.

Expected end of string, got 'LIMIT'

How should i use limit, and get the LAST 4 rows?

thanks!

like image 898
M Markero Avatar asked Dec 03 '25 14:12

M Markero


1 Answers

Use setMaxResults() to limit the number of results.

$course = $query->setMaxResults(4)->getResult();

If you want to use this for pagination you can add a setFirstResult() call.

 $course = $query->setMaxResults(4)->setFirstResult(10)->getResult();
like image 168
Nicolai Fröhlich Avatar answered Dec 06 '25 07:12

Nicolai Fröhlich