显示来自多个表的数据

时间:2011-05-16 12:41:56

标签: cakephp

我正在学习cakephp并做一个例子。我的数据库中有很多表,如User,Post,Comment,Friend,Photo,每个表都有一个外键user_id.Now我想显示用户的照片,他/她朋友的帖子,对这些帖子的评论,用户的朋友等成功登录后的任何社交网站。任何人请建议我如何做到这一点。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您希望使用包含行为来拉回包含所有这些字段的嵌套对象图。

http://book.cakephp.org/view/1323/Containable

你会得到这样的结果(......从蛋糕书中取出)

[0] => Array
    (
        [Post] => Array
            (
                [id] => 1
                [title] => First article
                [content] => aaa
                [created] => 2008-05-18 00:00:00
            )
        [Comment] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [post_id] => 1
                        [author] => Daniel
                        [email] => dan@example.com
                        [website] => http://example.com
                        [comment] => First comment
                        [created] => 2008-05-18 00:00:00
                    )
                [1] => Array
                    (
                        [id] => 2
                        [post_id] => 1
                        [author] => Sam
                        [email] => sam@example.net
                        [website] => http://example.net
                        [comment] => Second comment
                        [created] => 2008-05-18 00:00:00
                    )
            )
        [Tag] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [name] => Awesome
                    )
                [1] => Array
                    (
                        [id] => 2
                        [name] => Baking
                    )
            )
    )