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));
limit= 200 will not work as this will limit the record on a page but will retrieve the whole record..
Don't paginate with find().
Cake Pagination: http://book.cakephp.org/view/165/Controller-Setup
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.
"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
)
);