Doctrine - select last 4 rows from table

谁说胖子不能爱 提交于 2019-12-07 16:38:02

问题


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!


回答1:


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();


来源:https://stackoverflow.com/questions/19607277/doctrine-select-last-4-rows-from-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!