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 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里