简体   繁体   中英

How can I limit the find to a specific number in CakePHP?

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?

According to the documentation , the second argument to the find() method is a $params array.

One of the possible values to pass in this array is a limit key. So you could do the following:

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

"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"

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.

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;

So, $start = 0; $length = 20; // we are going to retrieve 20 records starting from the first record

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

You can also try this out

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

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

OR you can also try this out...

in your users controller set the $paginate to like this.

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

The records will be limited to 200 now wherever you use paginate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM