gpt4 book ai didi

cakephp - 计入包含 Cakephp 3

转载 作者:行者123 更新时间:2023-12-04 03:36:33 25 4
gpt4 key购买 nike

我有一张 table 发帖 这有一个 有很多 与表的关联 星星 .

我可以使用以下方法获取所有相关数据:

$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

这不会返回相关星的数量。

有什么问题还是应该以其他方式做?

谢谢

最佳答案

您还必须选择外键,否则蛋糕无法加入表格。你还必须对结果进行分组

'Stars' => function($q) {
$q->select([
'Stars.post_id',
'total' => $q->func()->count('Stars.post_id')
])
->group(['Stars.post_id']);

return $q;
}

关于cakephp - 计入包含 Cakephp 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36877486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com