-2

我基本上试图通过返回我的项目和更新表的用户和评论来扩展我的查询的深度。

站点访问者可以查看一个项目,该项目下面列出了许多更新。

用户可以对下面的初始项目和更新发表评论,并且每个更新和项目都由任何用户提交。

我建立了以下关系,但这些关系可能是错误的:

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

我的 ProjectController.php 中的视图如下:

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

但这只会导出一个包含以下内容的数组:

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

但我想要这个完整的结果,包含对所有内容的评论以及这些评论的用户:

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)

在数据库中,评论通过一个 ID (user_id) 与用户相关联,并且不包含关于用户的更多详细信息。

4

1 回答 1

5

你有没有试过$this->Project->recursive在打电话之前切换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

于 2012-07-10T16:24:41.997 回答