Reputation:

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?

Upvotes: 7

Views: 34271

Answers (6)

Ajanth
Ajanth

Reputation: 96

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

Upvotes: 2

Paolo Bergantino
Paolo Bergantino

Reputation: 488704

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

Upvotes: 13

Mike B
Mike B

Reputation: 32165

Don't paginate with find().

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

Upvotes: 3

You can also try this out

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

Upvotes: 1

Vicer
Vicer

Reputation: 1034

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

Upvotes: 9

Gaurav Sharma
Gaurav Sharma

Reputation: 2848

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.

Upvotes: -1

Related Questions