【发布时间】:2016-04-26 23:18:58
【问题描述】:
我有一张 Post 表格,它与表格 Stars 有 has-many 关联。
我可以使用以下方法获取所有相关数据:
$this->Posts->find()->contain(['Stars']);
效果很好。
但我想数星星。我已经尝试过了,但它不起作用:
$this->Posts->find->contain([
'Stars' => function($q) {
return $q->select(['total' => $q->func()->count('Stars.post_id')]);
}
]);
//I've also tried this
...
...$q->select(['total' => "COUNT(Stars.post_id)"]);
...
//Also fail
这不会返回关联的星数。
有什么不对还是应该以其他方式处理?
谢谢
【问题讨论】:
标签: cakephp cakephp-3.0