While working on a MediaLeaf project, I ran into an issue with pagination and associations.
Apparently, when you add a fourth association, the default pagination changes to the associated recursive functions. To get just one association that is needed, I had to to return all associations. Needleess to say, this is very cumbersome.
After tinkering for a bit, trying unbindModel, bindModel, and various recursive functions, I discovered the containable behavior. Contain is beautiful. You can modify your associations on the fly and return just the associated data you want.
Here’s how:
// use this to modify your model to be containable on the fly.
$this->Post->Behaviors->attach('Containable');
// only return the post data
$this->Post->contain();
// return tags data with your post
$this->Post->contain("Tag");
This stuff is great. Unfortunately, you have to take it a step further to use it with pagination. Contain has to be added to the pagination array (Hat Tip: Richard @ Home).
// use this to modify your model to be containable on the fly.
$this->Post->Behaviors->attach('Containable');
// now contain your pagination results
$this->paginate = array('order' => array('article_date desc'),
'limit' => 3,
'contain' => array("Tag"));
Give it a try and hopefully this helps you out.