cakephp paginate model with multiple association
I have three tables : Units, Offers and Agents with following associations
- Unit hasMany Offers
- Offer belongsTo Agent
When I paginate Unit table I get all the units and all the corresponding Offers for each unit , but not the Agent that offer belongs to. Ex:
- Unit1
- offer1
- offer开发者_StackOverflow中文版2
- Unit2
- offer1
- offer2
- offer3
And I want to have something like this:
- Unit1
- offer1
- agent1
- offer2
- agent2
- Unit2
- offer1
- agent1
- offer2
- agent2
- offer3
- agent3
How to do it?
The easiest solution is to increase the recursive of the Model before calling the paginage.
$this->Unit->recursive = 1; //or more
But you should be very careful, because you could call the same Model twice. Extra advice here: Try to unbind Unnecessary models before using this. Again - be very careful, because this could slowdown the app.
Using the Containable behaviour should get you there. I never use recursive now.
Alternatively, it can sometimes be helpful to create a database view across the tables of interest. This will make paging a lot simpler and will allow you to specify exactly what data you require (avoiding unnecessary data transfer).
精彩评论