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

I have a user model that gives me the latest data as output. How can I limit a record to only output 200 records, and not all users in the database?

+6
php cakephp
source share
6 answers

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

One of the possible values ​​passed in this array is the limit key. So you can do the following:

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

"It looks like an array to me ('limit' => 21, 'page' => 1) for paging 21 users on one page. If I change the limit to 200, then it splits pages into 200 pages on only one page .. . in this case, how to limit along with the correct pagination? - Anonymous

yes, you can use the pagination cakePHP helper as someone mentioned. But there may be times when you want to make your own pagination or simply limit the number of records received per call. Why is it here, how did I deal with this situation.

Say, for example, you want to get a certain number of entries on a page, then: $ start = 0; β†’ this is to start receiving recordings starting from the first. If you need to say, for example, start at the 31st, then $ start = 30;

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

And the code will look something like this:

 // To retrieve a number of Products per page $products = $this->Product->find('all', array( 'order' => 'product_number ASC', 'limit' => $start.','.$length, 'recursive' => -1 ) ); 
+8
source share
+3
source share
 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 convenient viewing on the pages. This can help

+2
source share

You can also try this.

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

open the user model file and do the following:

you will need to change the "limit" property in the relation 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 ...

in your user controller install $ paginate like this.

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

Entries will be limited to 200 now when you use paginate.

-one
source share

All Articles