【发布时间】:2015-08-24 10:42:13
【问题描述】:
我正在从事 CakePhp 3 项目,我需要按每条评论的投票总和对文章中的 cmets 进行排序。
型号:
- 文章
- 文章评论
- 文章评论投票
- 用户
用户与每个模型相关联。 ArticlesComments 具有文章 ID。 ArticlesCommentsVotes 有comment_id 和user_id。
我需要什么:
- 阳性 cmets 总数
- 负 cmets 总数
- 按阳性 cmets 号排序
- 按负数排序
不知何故,我设法得到了 Total no。正和负 cmets,但 CakePhp 不允许我按 cmets 的数量排序。
这是我的查询:
$article = $this->Articles->get($id, [
'contain' => [
'Categories',
'Clusters',
'Tags',
'ArticlesSteps',
'PositiveVotes' => function ($a){
return $a->select(['PositiveVotes.article_id', 'votes' => 'COUNT(*)']);
},
'NegativeVotes' => function ($a){
return $a->select(['NegativeVotes.article_id', 'votes' => 'COUNT(*)']);
},
'ArticlesComments' => function ($q){
return $q->contain([
'Users' => function ($q){
return $q->select(['username']);
},
'CommentVote' => function ($q){
return $q->select(['vote']);
},
'CommentsPositiveVotes' => function ($q){
return $q->select(['CommentsPositiveVotes.comment_id', 'positive' => 'SUM(vote)'])->group('comment_id');
},
'CommentsNegativeVotes' => function ($a){
return $a->select(['CommentsNegativeVotes.comment_id', 'CommentsNegativeVotes.user_id']);
}
]);
}
]
]);
任何帮助表示赞赏:)
【问题讨论】:
标签: php database cakephp associations cakephp-3.0