-2

I'm basically trying to extend the depth of my query by returning the Users and Comments for both my Project and Update tables.

Site visitors can view a Project, which has many Updates listed below it.

Users can leave comments on both the initial Project AND the Updates below, and each update and project is submitted by any users.

I have the following relationships set up, but these could be wrong:

Project.php (Model)
    belongsTo           - User
    hasMany             - Update, Comment

Update.php (Model)
    belongsTo           - Project, User
    hasMany             - Comment

Comment.php (Model)
    belongsTo           - Project, Update
    hasAndBelongsToMany - User

User.php (Model)
    hasMany             - Project, Update, Comment

The view in my ProjectController.php is as follows:

public function view($id = null, $slug = null) {
    $this->Project->id = $id;
    $this->set('Projects', $this->Project->read());
}

But this only exports an array containing:

Project
    User        (belonging to Project)
    Comments    (belonging to Project)
    Updates     (belonging to Project)

But I would like this full result, containing the comments to everything and the users for those comments:

Project
    User        (belonging to Project)
    Comments    (belonging to Project)
        User        (belonging to each Comment)
    Updates     (belonging to Project)
        User        (belonging to Update)
        Comments    (belonging to Update)
            User        (belonging to each Update Comment)

In the database, comments are tied to users by one ID (user_id) and contain no more details about the user than that.

tereško
  • 58,060
  • 25
  • 98
  • 150
Tim
  • 6,986
  • 8
  • 38
  • 57

1 Answers1

5

Have you tried toggling $this->Project->recursive before calling read()?

Depth   Description
-1      Cake fetches Group data only, no joins.
0       Cake fetches Group data and its domain
1       Cake fetches a Group, its domain and its associated Users
2       Cake fetches a Group, its domain, its associated Users, and the Users' associated Articles

http://book.cakephp.org/1.3/view/1063/recursive

Jay
  • 3,285
  • 1
  • 20
  • 19