CakePHP: Pagination with Containable
Took me a little while to work this out so I'm blogging it here in the hopes it will help someone in the future:
Need to Contain your Paginated data?
in your controller:
$this->paginate = array( "contain"=>array( // usual contain array ) ); $this->set("data", $this->paginate("Model"));
For example, to bring back and paginate a User (id, first_name, last_name) and their Title (Mr, Mrs, etc.):
/app/users_controller.php
$this->paginate = array( "contain"=>array( "id", "first_name", "last_name", "Title"=>array( "name" ) ) ); $this->set("data", $this->paginate("User"));
Happy Baking! :-)
- Richard@Home:


Comments
Well Done! Thanks for posting
Well Done!
Thanks for posting the code.
It consumed a lot of time searching to use containable with pagination.
Oops! You're absolutely right
Oops! You're absolutely right - good eyes! Fixed it. Thanks :-)
bug?
change:
$this->paginate(array(
to:
$this->paginate=array(
seems to be a bug (I run cake 1.2)
not a bug
this is what i do and it works.
$this->paginate('Model', $conditions);
set up your contain fields in var $paginate at the top of your controller.
var $paginate = array( 'limit' => 100, 'conditions'=>array(), 'contain'=>array('Model2'=>array( 'conditions'=>array(), 'Model3', 'Model4.field4', 'etc' ), 'order'=>array(), );
Post new comment