CakePHP选择由HAVING和AS抛出的计数

时间:2014-02-04 17:05:18

标签: php mysql cakephp max having

我正在开发一个包含存储桶的页面,这些存储桶几乎是我们系统中用户的容器。每个桶使用不同的条件和字段来获得不同类型的用户。

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

以下是我们调用getCount()后返回的错误。

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'most_recent' in 'having clause'

现在我意识到收到此错误是因为不包括字段数组但包括字段会破坏所有其他存储桶。我们尝试使用蛋糕$ this-> model-> find('count'),但这也不成功。

所以我不确定如何准确地计算计数而不将字段包含在计数中。

拒绝铲斗

'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);


    }

0 个答案:

没有答案