I'm a huge fan of cakephp's containable element, because I always thought, that it would handle loading of additional models appropriate. But in the last days I dug deeper and found out, that there's really an memory issue.
Think of the following model structure:
- Project has many Wall
- Project has many Participant
- Wall has many Post
- Post has many Comment
- Participant has many Post
- Participant has many Comment
- Participant belongs to User
- Participant belongs to Project
and vice versa
- Post belongs to Participant
- Post belongs to Wall
- Comment belongs to Participant
- Comment belongs to Post
- User has many Participant
In the wall-Controller I have following find-Statement:
$this->set(
"posts",
$this->Post->find(
"all", array(
"conditions" => array("Post.wall_id" => $wall["Wall"]["id"]),
"contain" => array("Participant")
)
)
);
I would expect, that cakephp would find all posts and include only the corresponding participant-objects. But, what I get is a list of all Posts (correct) & their Participants (correct) but also of the corresponding Wall (incorrect) & the corresponding Comments, if available (incorrect). So from performance point of view: way too much objects, which can lead to a "FATAL ERROR - memory overload".
And for the, theoratically really sexy & interesting part:
$this->set(
"posts",
$this->Post->find(
"all", array(
"conditions" => array("Post.wall_id" => $wall["Wall"]["id"]),
"contain" => array("Participant.User")
)
)
);
Because I'm only interested in the Post & Participant.User objects, I change the contain-array to Participant.User. But, again, now I'm getting not only the User object, but also all other related objects to the Participant (project, posts, comments, ...) and the object-tree is much bigger than before.
So I was wondering, what is the correct way to implement this? Do I need to explicitly set the "join" option or do I have to set the fields option (in the root or in the contain-option)?
Greets from Austria.