php 如何在 CakePHP 中将查找限制为特定数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861960/
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
How can I limit the find to a specific number in CakePHP?
提问by Vicer
I have a user model which gives me latest users as output. How can I limit the record to just output me 200 records instead of all the users in database?
我有一个用户模型,它为我提供最新的用户作为输出。如何将记录限制为仅输出 200 条记录而不是数据库中的所有用户?
回答by Paolo Bergantino
According to the documentation, the second argument to the find()method is a $paramsarray.
根据文档,该find()方法的第二个参数是一个$params数组。
One of the possible values to pass in this array is a limitkey. So you could do the following:
传入此数组的可能值之一是limit键。因此,您可以执行以下操作:
$users = $this->User->find('all', array('limit' => 200));
回答by Vicer
"i have it like array('limit' => 21, 'page' => 1) for paging 21 users in one page.. if i change the limit there to 200 then it paginates 200 users in one page only...in this case how to limit along with proper pagination?? – Anonymous May 14 '09 at 7:22"
“我有它像 array('limit' => 21, 'page' => 1) 用于在一页中分页 21 个用户..如果我将那里的限制更改为 200 那么它只在一页中分页 200 个用户......在这种情况下,如何限制并正确分页? – Anonymous May 14 '09 at 7:22"
yes you can use the cakePHP pagination helper as someone has mentioned. But there may be some cases where you want to do your own pagination or just limit the number of records retrieved per call. For what it's worth here's how I handled one such situation.
是的,您可以使用有人提到的 cakePHP 分页助手。但在某些情况下,您可能想要自己进行分页或仅限制每次调用检索的记录数。值得一提的是,我是如何处理这种情况的。
Say for example you want to retrieve a certain number of records per page, Then: $start = 0; -> this is in order to start retrieving records starting from the first one. If you need to say for example, start from the 31st, then $start = 30;
比如说你想每页检索一定数量的记录,那么: $start = 0; -> 这是为了从第一个开始检索记录。如果你需要说例如从31号开始,那么$start = 30;
So, $start = 0; $length = 20; // we are going to retrieve 20 records starting from the first record
所以,$start = 0; $length = 20; // 我们将从第一条记录开始检索 20 条记录
And the code will be something like:
代码将类似于:
// To retrieve a number of Products per page
$products = $this->Product->find('all', array(
'order' => 'product_number ASC',
'limit' => $start.','.$length,
'recursive' => -1
)
);
回答by Mike B
Don't paginate with find().
不要使用 find() 进行分页。
Cake Pagination: http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
蛋糕分页:http: //book.cakephp.org/2.0/en/core-libraries/components/pagination.html
回答by Ajanth
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
//array of field names
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
//string or array defining order
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
)
Limit * page = 200 set your values according to your comfortable view in pages. This might help
Limit * page = 200 根据您在页面中的舒适视图设置您的值。这可能有帮助
回答by Shanmugasundaram Chinnananchi.
You can also try this out
你也可以试试这个
$results = $this->Model->find('all',
array('limit'=>10,
'order'=>array('date DESC')));
回答by Gaurav Sharma
open the model file of user and do as follows:
打开用户的模型文件,进行如下操作:
you will need to change the 'limit' property in the relationship variable named
您将需要更改名为的关系变量中的“限制”属性
var $hasMany = array( 'Abus' => array('className' => 'Abus', 'foreignKey' => 'user_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '200','offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '' ) );
var $hasMany = array( 'Abus' => array('className' => 'Abus', 'foreignKey' => 'user_id', 'dependent' => false, 'conditions' => '', 'fields' = > '', 'order' => '', 'limit' => '200','offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''));
OR you can also try this out...
或者你也可以试试这个......
in your users controller set the $paginate to like this.
在您的用户控制器中将 $paginate 设置为这样。
var $paginate = array('limit' => 200);
var $paginate = array('limit' => 200);
The records will be limited to 200 now wherever you use paginate.
现在无论您在何处使用分页,记录都将限制为 200。

