我不确定问题标题,但试图在下面解释更多。提前感谢所有帮助我的人。
我对表格进行了抽样以简化我的问题。
数据库表
- 新闻:news_id,标题
- 评论:comment_id、news_id、内容、创建(时间戳)
用户正在对新闻发表评论。在输入“日期间隔 01.10.2015 - 05.11.2015”的搜索中,结果应包括:在此间隔之间已评论的新闻。并且comment_count 应该显示在屏幕上的标题旁边。这个comment_count 不是整个comment_count,只是这个日期之间的计数。
为此,我首先在评论中运行了查询。然后统计评论。然后按 news_id 分组。
$Data = $this->Comment->find('all', array(
'fields' => array(
'News.news_id','News.title'
),
'conditions' => array(
"Comment.created >=" => date('Y-m-d', strtotime("-1 days")) //yesterday's commented news
),
'contain' => array(
'News' => array(
'Comment => array(
'fields' => array('COUNT(Comment.id) as comment_count'),
)
)
),
'group' => array('News.title')
));
结果如下,嵌套过多:
Array
(
[0] => Array
(
[News] => Array
(
[id] => 27
[title] => sample news title
[News] => Array
(
[id] => 27
[title] => sample news title
[Comment] => Array
(
[0] => Array
(
[News_id] => 27
[Comment] => Array
(
[0] => Array
(
[Comment_count] => 1
)
)
)
)
)
)
)
)
总之,要达到comment_count,我必须手动编写嵌套。但是我想在第一个 [title] 节点的同一级别上达到它。
$JsonData [] = array(
"id" => $Item["News"]["id"],
"title" => $Item["News"]["title"],
"comment_count" => $Item["News"]["News"]["Comment"][0]["Comment"][0]["comment_count"]
);
如何减少终身筑巢?
提前致谢。