我有一个表Post,这与表Stars 有很多联系.
我可以使用以下方法获取所有相关数据:
$this->Posts->find()->contain(['Stars']);
Run Code Online (Sandbox Code Playgroud)
这很好用.
但我想数星星.我试过这个但它不起作用:
$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
Run Code Online (Sandbox Code Playgroud)
这不会返回关联星数.
有什么不对或应该采取其他方式吗?
谢谢