【问题标题】:Cakephp count containable recordsCakephp 计数可包含的记录
【发布时间】:2015-12-09 06:06:35
【问题描述】:

Cakephp 2.6

Schools hasMany Pupils,我正在运行来自 Schools 模型的可包含查询。在这个查询中,我想计算每所学校的学生人数,但我无法让它发挥作用。

我尝试过使用以下虚拟字段,但它不起作用

$this->virtualFields['total'] = 'COUNT(Pupil.id)'; 以下是不准确的计数

$contain = array(
            'Pupil' => array(
                'fields' => array(
                    'CONT(Pupil.id) AS total'
                )
            ),
        );

我不想使用counterCache,因为数据可以大量导入到框架之外的两个表中,所以我不能依赖它来更新计数字段。 我怎样才能让这个计数正确运行?

【问题讨论】:

    标签: php mysql cakephp


    【解决方案1】:

    尝试按Schools添加分组:

        $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...
            ),
        );
    

    【讨论】:

    • 现在出现错误Model Pupil is not associated with model Pupil
    • 哦,Cake 2 好像不支持分组...查看here 了解更多信息
    • 我尝试了手动加入,但也无法正常工作。我将如何正确构建它?
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 2012-03-01
    • 2015-04-03
    • 2014-06-04
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 2013-05-07
    相关资源
    最近更新 更多