Got some CakePHP models with deep associations that you need to pull into the data that you display when you paginate them? This used to be a royal pain in the butt, and pretty much required custom pagination, but no more!
Behold:
$this->paginate = array( 'Customer' => array( 'contain' => array( 'Order(id)', 'Order.AccessCode(id)', 'ShippingAddress(city,state)' ) ) ); $this->set('customers', $this->paginate('Customer'));
All you need to do for this to work? Make sure you have your associations defined, and add the Containable behavior to each of the models in question. It works flawlessly, and can go as deep as you need, and retrieve just the fields you need. I haven’t tested sorting on deep relationships yet, but I don’t think that works, because of the way Cake does its queries. But still, this is better than nothing.









Your method is good for pagination only, but what about the sorting? The paginate method of cake does not work for derived fields.
However I’ve got the solution and here is the details: http://abcoder.com/php/cakephp/cakephp-advanced-pagination-sort-by-derived-field/
Hope you’d like it
Thanks
Adnan
I hadn’t run into the derived fields bug. Thanks for the information.