【问题标题】:display data from more than one table显示来自多个表的数据
【发布时间】:2011-05-17 03:25:41
【问题描述】:

我正在学习cakephp并做一个例子。我的数据库中有很多表,例如用户,帖子,评论,朋友,照片,每个表都有一个外键user_id。现在我想显示用户的照片,他/她成功登录后,朋友的帖子,这些帖子上的 cmets,用户的朋友等,就像任何社交网站一样。有人可以建议我如何做到这一点。

提前致谢。

【问题讨论】:

标签: cakephp


【解决方案1】:

您想使用 Containable Behavior 拉回包含所有这些字段的嵌套对象图。

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

你会得到这样的结果(...从 cakephp 书中提取)

[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
                    )
            )
    )

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多