CakePHP选择使用和作为抛出的计数

CakePHP选择使用和作为抛出的计数,php,mysql,cakephp,max,having,Php,Mysql,Cakephp,Max,Having,我正在开发一个包含bucket的页面,bucket是我们系统中用户的容器。每个bucket使用不同的条件和字段来获取不同类型的用户 到目前为止,桶、条件、字段和其他所有内容都存储在一个巨大的数组中。我们希望这会使添加桶变得相对简单,但现在我们还得到了每个桶中的客户数量,这导致我们的最新桶出现问题(已拒绝) 这是调用getCount()后返回的错误。 Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'most_recent

我正在开发一个包含bucket的页面,bucket是我们系统中用户的容器。每个bucket使用不同的条件和字段来获取不同类型的用户

到目前为止,桶、条件、字段和其他所有内容都存储在一个巨大的数组中。我们希望这会使添加桶变得相对简单,但现在我们还得到了每个桶中的客户数量,这导致我们的最新桶出现问题(已拒绝)

这是调用getCount()后返回的错误。

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'most_recent' in 'having clause'
现在我意识到,我们之所以会出现此错误,是因为我们没有包含字段数组,但包含字段会打断所有其他存储桶。我们尝试使用$this->model->find('count'),但也没有成功。

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'most_recent' in 'having clause'
因此,我不确定如何在不将字段包含到计数中的情况下准确获取计数

被拒绝的桶

'Declined' => array(
                'color' => '#33CC33',
                'conditions' => array(
                    'Program.deal_status' => 'declined',
                    "Customer.sales_associate {CONDITION}",
                    "Program.date_submitted > DATE_SUB(CURDATE(), INTERVAL 60 DAY)",
                    'Customer.store {STORE_CONDITION}',
                ),
                'joins' => array(
                    array(
                        'table' => 'programs',
                        'alias' => 'Program',
                        'type' => 'LEFT',
                        'conditions' => array(
                            'Program.customer_id = Customer.customer_id'
                        )
                    ),
                    array(
                        'table' => 'activities',
                        'alias' => 'Activity',
                        'type' => 'LEFT',
                        'conditions' => array(
                            'Activity.customer_id = Customer.customer_id',
                        )
                    ),
                    array(
                        'table' => 'customer_interactions',
                        'alias' => 'CustomerInteraction',
                        'type' => 'LEFT',
                        'conditions' => array(
                            'CustomerInteraction.customer_id = Customer.customer_id'
                        )
                    )
                ),
                'order' => array(
                    'Program.date_submitted DESC',
                ),
                'group' => array(
                    'Activity.customer_id HAVING most_recent NOT BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) and DATE_ADD(NOW(), INTERVAL 30 DAY)',
                ),
                'fields' => array(
                    'customer_id' => 'DISTINCT Customer.customer_id',
                    'Customer' => "CONCAT_WS(' ', CustomerPersonalInformation.first_name, CustomerPersonalInformation.last_name) AS full_name",
                    'Email' => 'CustomerContactInformation.email',
                    'Last Interaction' => 'CustomerInteraction.notes',
                    'Program Date' => 'Program.date_submitted',
                    'Underwriter notes' => 'Program.underwriters_notes',
                    'Last Activity Date' => 'Activity.date',
                    'hidden' => 'MAX(Activity.date) AS most_recent',
                )
            ),
获取计数:

public function getCount($department, $table, $employeeId = 'all',  $store = 'all'){


        $this->layout = 'blank';

        $bucket = $this->buckets[$department][$table];

        switch ($department) {
            case 'bdc':
                if ($employeeId === 'all'){
                    $storeConditionReplace = ($store === 'all') ? "IS NOT NULL" : " = '$store'";
                    $conditionReplace = "IS NOT NULL";
                    break;
                }
                $conditionReplace = " = '$employeeId'";
                break;
            case 'sales':
                if ($employeeId === 'all'){
                    $storeConditionReplace = ($store === 'all') ? "IS NOT NULL" : " = '$store'";
                    $conditionReplace = "IS NOT NULL";
                    break;
                }
                $conditionReplace = " = '$employeeId'";
                break;
        }

        foreach ($bucket['conditions'] as &$condition) {
            if(is_array($condition)){
                continue;
            }
            $condition = str_replace('{CONDITION}', $conditionReplace, $condition);
            if (isset($storeConditionReplace))
                $condition = str_replace('{STORE_CONDITION}', $storeConditionReplace, $condition);
            elseif (strpos($condition, '{STORE_CONDITION}') !== false)
                $condition = null;
        }
        $result = $this->Customer->find('count', array(
                'conditions' => array(
                    $bucket['conditions'],

                ),
                'fields' => array(
                    'COUNT(DISTINCT Customer.customer_id)'
                ),  
                'group' => ((isset($bucket['group'])) ? $bucket['group'] : null),
                'order' => array(
                    $bucket['order'],
                ),
                'contain' => array(
                    'CustomerPersonalInformation',
                    'CustomerMarketingOption',
                    'CustomerContactInformation'
                ),
                'joins' => $bucket['joins']
            )
        );
        $this->set('count',$result);


    }

请显示生成的sql查询。