我有一个包含 id、template_id 和 destination_id 的数据库表。我想计算每个destination_id 中的template_id 的数量。因此,我得到了一个数组,其中包含目标中每个模板的总计。
在模型中我放置了这个:
var $virtualFields = array(
'template_count' => 'COUNT(Content.template_id)'
);
我确实得到了每个模板的计数。只是我不知道如何获得总数
最佳答案
我不会在基于聚合函数的模型中定义虚拟字段。
我认为您需要的是一个如下所示的查询:
SELECT Content.destination_id, COUNT(Content.template_id) AS template_count
FROM contents AS CONTENT
GROUP BY Content.destination_id
尝试使用此 Cakephp 代码:
$this->find('all' array(
'fields' => array('Content.destination_id'
, 'COUNT(Content.template_id) AS template_count'),
'group' => array('Content.destination_id')));
关于php - CakePHP 计算某个父级中具有 ID 的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27111234/