Cakephp 2.6
Schools hasMany小学生,我正在运行一个来自Schools模型的可包含查询。在这个查询中,我想计算每个学校的学生人数,但是我不能使它工作。
我已经尝试使用以下虚拟字段,但它不工作
$this->virtualFields['total'] = 'COUNT(Pupil.id)';
下面给出了不准确的计数
$contain = array(
'Pupil' => array(
'fields' => array(
'CONT(Pupil.id) AS total'
)
),
);
我不想使用counterCache
作为数据可以大量导入到框架外的两个表,所以我不能依赖它来更新计数字段。我怎样才能使这个计数正确地运行?
尝试通过School
s添加分组:
$contain = array(
'Pupil' => array(
'fields' => array(
'CONT(Pupil.id) AS total'
),
'group' => array('Pupil.school_id') // doesn't really know how you called `school` relation column...
),
);