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

报告相同问题?

悬赏问题

  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏
  • ¥15 Wanted but not invoked:Actually, there were zero interactions with this moc