Limit amount of records retrieved when using Doctrine DQL in Symfony2(在 Symfony2 中使用 Doctrine DQL 时限制检索的记录数量)
问题描述
我有以下查询:
$latestcontent = $em->createQuery('
SELECT c.title, c.content, c.lastedit, a.firstname, a.surname
FROM ShoutMainBundle:Content c, ShoutMainBundle:Admin a
WHERE c.author = a.id
ORDER BY c.lastedit ASC'
);
我需要做的是限制从此查询返回的记录数量.但是,当我将 LIMIT 10 添加到 SQL 查询时,它返回此错误:
What I need to do, is limit the amount of records returned from this query. However, when I add LIMIT 10 to the SQL query, it returns this error:
错误:预期的字符串结尾,得到LIMIT".
Error: Expected end of string, got 'LIMIT'.
所以,我看了一下,发现您可以将 ->limit(10)
添加到代码中(在查询之后).但这会引发这个 PHP 错误:
So, I had a look and found that you could do add ->limit(10)
to the code (after the query). But this then throws up this PHP error:
Fatal error: Call to undefined method DoctrineORMQuery::limit() in C:wampwwwsrcShoutAdminBundleControllerDefaultController.php on line 22
我做错了什么?
推荐答案
据我所知,目前没有像 LIMIT 这样的 DQL 声明.
There is no statement like LIMIT for DQL currently, as far as I know.
你必须使用 Query::setMaxResults().
这篇关于在 Symfony2 中使用 Doctrine DQL 时限制检索的记录数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Symfony2 中使用 Doctrine DQL 时限制检索的记录数量


基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01