php 如何限制学说 2 中结果集的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5640768/
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-25 22:03:04  来源:igfitidea点击:

How to limit size of result set in doctrine 2?

phpormdoctrine-orm

提问by blacktie24

If i'm using the findBy method of the respository class, how can I limit the size of the result set?

如果我使用 respository 类的 findBy 方法,我该如何限制结果集的大小?

回答by Nikolai Senkevich

In Doctrine 2.1 method EntityRepository#findBy() now accepts additional parameters for ordering, limit and offset.

在 Doctrine 2.1 方法 EntityRepository#findBy() 现在接受用于排序、限制和偏移的附加参数。

see full list new features in doctrine 2.1(404) Relevant link to findBy and findOneBy

见学说 2.1(404) 中的 完整列表新功能findBy 和 findOneBy 的相关链接

example:

例子:

 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)

usage:

用法:

$product = $repository->findBy(
    array('name' => 'foo'),
    array('price' => 'ASC'),
    $myLimit,
    $myOffset
);

回答by Jarzon

For Doctrine Query Languageyou have:

对于Doctrine 查询语言,您有:

QueryBuilder::setMaxResults(integer $maxResults)

回答by timdev

The findBy() method of the generic repository class doesn't support this.

通用存储库类的 findBy() 方法不支持这一点。

I would write your own repository (as outlined here) and override findBy() to take additional parameters. Your new implementation could use the query builder, or plain-old-DQL to build up the proper query. (I'd use the querybuilder, as you can probably just pass the $critera param right into QueryBuilder::where())

我会编写您自己的存储库(如此处所述)并覆盖 findBy() 以获取其他参数。您的新实现可以使用查询构建器或普通 DQL 来构建正确的查询。(我会使用查询构建器,因为您可能只需将 $ critera参数直接传递给QueryBuilder::where()