Lyman
Lyman

Reputation: 79

How to set pagination from find results in cakephp?

I have a code on my Ticket controller that displays all of the tickets created by the logged in user:

 $this->set('tickets',$this->Ticket->find('all',array('conditions'=>array('user_id'=>$this->Auth->user('id')))), $this->paginate());

It returns correct results, however, my pagination (when you view the index page) shows all the tickets created by all users (Page 1 of 1, showing 18 records out of 18 total, starting on record 1, ending on 18). The code above should only return 3 tickets when you view the index page.

Thanks in advance.

Lyman

Upvotes: 0

Views: 3115

Answers (1)

OpenSorceress
OpenSorceress

Reputation: 2014

Like this:

// Contoller - define pagination settings
     var $paginate = array(
                'Tickets'=>array(     
                     'limit' => 3, 
                     'order' => array( 'User.id' => 'asc' ) // or whatever
                     )
                  );

Now call $this->paginate instead of $this->find.

-- Editing to include conditions to find.

// The second argument takes your conditions. Model.field syntax is preferable.
$this->set('tickets', $this->paginate('Tickets', array("Tickets.user_id = {$this->Auth->user('id')}"));

See http://book.cakephp.org/view/1232/Controller-Setup and http://book.cakephp.org/view/1237/Custom-Query-Pagination

Upvotes: 1

Related Questions