cakephp完全查找/读取深度

时间:2012-07-10 16:20:56

标签: php cakephp cakephp-appmodel

我基本上是通过返回Project和Update表的Users和Comments来扩展查询的深度。

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

用户可以在下面的初始项目和更新中留下评论,每个更新和项目都由任何用户提交。

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

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)与用户绑定,并且不包含有关用户的详细信息。

1 个答案:

答案 0 :(得分: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