如何在CakePHP中将查找限制为特定数字?

时间:2009-05-14 07:12:03

标签: php cakephp

我有一个用户模型,可以将最新用户作为输出。如何限制记录只输出200条记录而不是数据库中的所有用户?

6 个答案:

答案 0 :(得分:12)

According to the documentationfind()方法的第二个参数是$params数组。

传递此数组的一个可能值是limit键。所以你可以做到以下几点:

$users = $this->User->find('all', array('limit' => 200));

答案 1 :(得分:8)

“我喜欢它的数组('limit'=> 21,'page'=> 1),用于在一个页面中分页21个用户..如果我将限制更改为200,那么它会分页200用户只在一个页面...在这种情况下如何限制与适当的分页?? - 匿名2009年5月14日在7:22“

是的,你可以像有人提到的那样使用cakePHP分页助手。但是在某些情况下,您可能希望自己进行分页或仅限制每次调用检索的记录数。这里有什么值得我处理这种情况的方法。

比如说你想要每页检索一定数量的记录,然后: $ start = 0; - >这是为了从第一个开始检索记录。如果您需要说,例如,从31开始,然后$ start = 30;

所以, $ start = 0; $ length = 20; //我们将从第一条记录

开始检索20条记录

代码将类似于:

// To retrieve a number of Products per page
            $products = $this->Product->find('all', array(
                                            'order' => 'product_number ASC',
                                            'limit' => $start.','.$length,
                                            'recursive' => -1
                                            )
                                        );

答案 2 :(得分:3)

请勿使用find()进行分页。

Cake Pagination:http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

答案 3 :(得分:2)

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
)

限制*页面= 200根据您在页面中的舒适视图设置您的值。这可能会有所帮助

答案 4 :(得分:1)

你也可以试试这个

$results = $this->Model->find('all', 
       array('limit'=>10, 
             'order'=>array('date DESC')));

答案 5 :(得分:-1)

打开用户的模型文件并执行以下操作:

您需要更改名为

的关系变量中的'limit'属性
  

var $ hasMany = array('Abus'=>   数组('className'=>'Abus',         'foreignKey'=> '用户身份',         'dependent'=>错,'条件'   => ',             'fields'=> '','order'=> '','限制'=> '200','抵消'   => '','exclusive'=> '','finderQuery'=> ',         'counterQuery'=> ''));

或者你也可以尝试一下......

在您的用户控制器中

将$ paginate设置为这样。

  

var $paginate = array('limit' => 200);

现在,无论您在何处使用分页,记录都将限制为200。