CakePHP使用选择条件从多个表中检索数据

时间:2014-09-22 09:44:31

标签: php mysql cakephp

我正在使用CakePHP构建应用程序,并且我遇到了使用一系列连接检索数据的问题。在下面的简化示例中,使用别名Delivery的连接可能具有多个记录,并且我希望在该表的特定字段中返回具有最大值的记录。

$inv_conditions = array(    'Invoice.invoice_date >=' => $DateFormatter->dateForDB($dateFrom), 
                 'Invoice.invoice_date <=' => $DateFormatter->dateForDB($dateTo),
                 'Invoice.id >' => 385380 );    

$join   =   array(array(
                'table' => 'jobs',
                'alias' => 'Jobs',
                'type'  => 'LEFT',
                'conditions'    => array('Invoice.job_id = Jobs.JOB_ID' ) 
              ),    
            array(
                'table' => 'functional',
                'alias' => 'Delivery',
                'type'  => 'LEFT'
                'conditions'=> array('AND ' => array('Invoice.job_id = Delivery.JOB',
                                                     'Delivery.TYPE_STAGE = 1') 
                            )
                     )

        );    

$invoices = $this->Invoice->find("all", array(
                "fields"    =>  array(
                            'Invoice.id',
                            'Invoice.job_id',
                            'Invoice.invoice_no',
                            'Invoice.consolidated_type',
                            'Invoice.customer_id_tbc',
                            'Invoice.invoice_date',
                            'Invoice.invoice_reference',
                            'Invoice.invoice_req',
                            'Jobs.PAYMENT_TYPE',
                            'Jobs.CUSTOMER',
                            'Jobs.MOST_RELEVANT_LINE',
                            'Delivery.DEPARTURE_DATE',
                            'Delivery.CNOR_CNEE_NAME',
                            'Delivery.TOWN_NAME',
                              ),
                "conditions"    =>  $inv_conditions,
                "joins"     => $join
                )
            );

}

我可以用SQL做到这一点没有问题如下:

SELECT 
    jobs.JOB_ID, 
    jobs.CUSTOMER, 
    functional.JOB_LINE_ORDER, 
    functional.CNOR_CNEE_NAME, 
    functional.TOWN_NAME 
FROM jobs JOIN functional ON
            jobs.JOB_ID = 'M201409180267'
        AND 
            functional.JOB =  jobs.JOB_ID
        AND 
            functional.TYPE_STAGE = 0 
        AND 
            functional.JOB_LINE_ORDER = 
               (SELECT MAX(JOB_LINE_ORDER) FROM functional 
                WHERE functional.JOB = 'M201409180267' AND functional.TYPE_STAGE = 0) 

我已尝试将以下内容用于条件数组:

'conditions'    =>    array('AND ' => 
                           array( 'Invoice.job_id = Delivery.JOB',
                                  'Delivery.TYPE_STAGE = 1'
                                  'Delivery.JOB_LINE_ORDER = MAXIMUM(Delivery.JOB_LINE_ORDER)' )
                            )

这确实会带来结果但不会返回正确的结果,而Cake生成的结果SQL在where子句中有一个select。有没有办法在Cake中检索数据时执行此操作,其中创建的sql语句将在where子句中具有select。

任何建议都将不胜感激。 谢谢 BAS

1 个答案:

答案 0 :(得分:0)

您需要使用子查询在where子句中生成select。您可以在模型中创建一个执行此操作的方法,并从控制器中调用它。

$subQuery = $db->buildStatement(
            array(
                'fields'     => array(''),
                'table'      => $db->fullTableName($this),
                'alias'      => 'aliasName',
                'limit'      => null,
                'offset'     => null,
                'joins'      => array(),
                'conditions' => $conditionsArray,
                'order'      => null,
                'group'      => null
            ),
            $this
        );
        $subQuery = ' <<YOUR MAIN QUERY CONDITION (' . $subQuery . ') >>';
        $subQueryExpression = $db->expression($subQuery);

        $conditions[] = $subQueryExpression;
        return $this->Model->find('list', compact('conditions'));