我有模型用户、帖子、评论和标签。
- 用户创建帖子。
- 帖子可以有多个评论和标签。
每个模型都有自己的表格,因此有表格“帖子”、“评论”和“标签”。评论有一个外键叫做'post_id',而标签有一个叫做'post_tags'的many_to_many关系表,其中有两个字段:'post_id'和'tag_id'。
我想得到一个嵌套数组,如下所示:
- 我应该运行哪些 MySQL 查询?
- 我想我需要用 PHP 改变结果来获得我的嵌套数组。如何?
非常感谢你的帮助 :-)
[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
)
)
)
[1] => Array
(
[Post] => Array
(...