dongsui4658 2014-09-22 09:44
浏览 42
已采纳

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

I am building an application using CakePHP and I am stuck on a problem retrieving data using a series of joins. In the simplified example below the join with the alias Delivery could have more than record and I want to bring back the record with a max value in a particular field in that table.

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

}

I can do this with SQL no problem as follows:

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) 

I have tried using the following to the conditions array:

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

This does bring back results but not the correct ones and the resulting SQL generated by Cake does have a select in the where clause. Is there a way of doing this when retrieving data in cake where the sql statement created will have a select in the where clause.

Any suggestions would be greatly appreciated. Thanks Bas

  • 写回答

1条回答 默认 最新

  • douxuanma4357 2014-09-22 10:01
    关注

    You need to use subquery to generate the select in the where clause. You can create a method in your model that does this and call it from your controller.

    $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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘