0

Here's the code I'm using to get the records. I need to know is there any thing like find first 100 or top 100 records and then paginate it. the db will contain 1000's of records ;)

$options = array(
        'limit' => 10,
        'recursive' => 0
        );
$this->paginate = $options;
$this->set('movies',$this->paginate());

Everything working fine, But I need just top 100 records and pagination only for that 100 records. I searched in many places, I couldn't find. Hope someone will help me :)

praveen kumar
  • 828
  • 4
  • 14
  • 22

2 Answers2

3

Try combine limit and order options together to get results.

array(
    'order' => array('created' => 'desc')
    'limit' => 100,
)

Here 'created' is timestamp table column. If saying "top" you mean latest records then in order you should use 'desc', otherwise 'asc'.

UPDATE

My previous sample is for find function, not for paginate. Seems you looking how use paginate for records subset. There no build-in option for that right now. You should take a look for other answer to see if solution works for you.

Community
  • 1
  • 1
user9440008
  • 572
  • 1
  • 9
  • 21
  • Actually when I give limit as 100, I'm getting pagination as 100 per page. I'm using the order for other field. – praveen kumar Aug 22 '12 at 06:56
  • @praveenkumar I was misunderstood your original idea. See my updated answer. – user9440008 Aug 22 '12 at 11:26
  • Thanks :) It worked, But its having some bugs. Like I have only 20 records, But I have mentioned 100 as totallimit. The pagination is generated for 100 records even though there is only 20 records. Any help ? – praveen kumar Aug 22 '12 at 13:28
  • @praveenkumar If you not sure about database records number, you could make additional check by using find('count') and set that value to totallimit if it will be lower than 100. It will be additional database check (which cost performance) but it is a "price" of workaround which you working on. – user9440008 Aug 22 '12 at 14:23
0

Create a custom find method for top 100, you can use that with pagination:

example: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-custom-find

Ceeram
  • 748
  • 4
  • 9