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
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(), );
Oops! You're absolutely right
Oops! You're absolutely right - good eyes! Fixed it. Thanks :-)
Well Done! Thanks for posting
Well Done!
Thanks for posting the code.
It consumed a lot of time searching to use containable with pagination.
Help using paginator
Hello
Im getting this error message when trying to paginate.
Warning (512): Controller::paginate() - can't find model equipos in controller EquiposController [CORE\cake\libs\controller\controller.php, line 1104]
I've included this var in the controller
var $paginate = array('limit' => 25);
This is the function in the controller (line with arrow)
function index1()
{
$this->set('equipos',$this->Equipo->find('all'));
---->> $data = $this->paginate('equipos');
$this->set('equipos', $data);
}
Anyone has a idea of whats happening????
thx in advance......
Post new comment