php 在 Symfony2 中使用 Doctrine DQL 时限制检索的记录数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7404133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:37:24  来源:igfitidea点击:

Limit amount of records retrieved when using Doctrine DQL in Symfony2

phpsqldoctrine-ormsymfonydql

提问by mickburkejnr

I have the following query:

我有以下查询:

   $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'
            );

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 10 添加到 SQL 查询时,它返回此错误:

Error: Expected end of string, got 'LIMIT'.

错误:预期的字符串结尾,得到“LIMIT”。

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:

所以,我看了一下,发现你可以添加->limit(10)到代码中(在查询之后)。但这会引发这个 PHP 错误:

Fatal error: Call to undefined method Doctrine\ORM\Query::limit() in C:\wamp\www\src\Shout\AdminBundle\Controller\DefaultController.php on line 22

What am I doing wrong?

我究竟做错了什么?

回答by Raffael

There is no statement like LIMIT for DQL currently, as far as I know.

据我所知,目前没有像 DQL 的 LIMIT 这样的声明。

You have to use Query::setMaxResults().

您必须使用Query::setMaxResults()