在cakephp上加入3个表

时间:2017-07-26 08:46:39

标签: sql cakephp

我尝试使用cakephp加入3个表但我不明白为什么当我执行查询时,主表的结果没有来自其他2个表的属性。这是我的PHP代码:

$flightSchedule = $this->FlightSchedules->find('all', array(
    'join' => array(
        array(
           'table' => 'WeeklySchedules',
           'alias' => 'ws',
           'type' => 'INNER',
           'conditions' => array(
           'FlightSchedules.code = ws.flight_plan_code'
        ),
        array(
           'table' => 'Structures',
           'alias' => 's',
           'type' => 'LEFT',
           'conditions' => array(
           'FlightSchedules.code = s.flight_plan_code',
       )),
    )),
    'conditions' => array(
        'FlightSchedules.plane_code' => 'xxx'
     ),
));

此结果始终是FlightSchedules记录,没有其他表的连接。任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

使用“fields”显示关联表的数据。

  $flightSchedule = $this->FlightSchedules->find('all', array(
            'join' => array(
                array(
                   'table' => 'WeeklySchedules',
                   'alias' => 'ws',
                   'type' => 'INNER',
                   'conditions' => array(
                   'FlightSchedules.code = ws.flight_plan_code'
                ),
                array(
                   'table' => 'Structures',
                   'alias' => 's',
                   'type' => 'LEFT',
                   'conditions' => array(
                   'FlightSchedules.code = s.flight_plan_code',
               )),
            )),
            'conditions' => array(
                'FlightSchedules.plane_code' => 'xxx'
             ),
        'fields'=>'FlightSchedules.*,ws.*,s.*'

        ));

答案 1 :(得分:0)

condition array

之后添加此内容
'fields' => 'FlightSchedules.*, WeeklySchedules.*, Structures.*'