在cakephp中访问相关模型/表到内连接查询

时间:2014-06-19 07:05:11

标签: php mysql sql cakephp

我在名为inner的分页模型中有join Application个查询

Application有很多AssignedExploration

AssignedExploration属于Exploration

Exploration属于ExplorationCategory

ExplorationCategory属于Season

到目前为止,我的查询是:

$session_condition = array(
    'table' => 'assigned_explorations',
    'alias' => 'AssignedExploration',
    'type' => 'INNER',
    'conditions' => array(
        'AssignedExploration.application_id = Application.id',
        'OR' => array(
            array('AssignedExploration.label' => $this->request->named['filter_session'])
        )
    )
);

我想在OR内添加另一个查询,applications Season.name = fall

我尝试添加array('Season.name' => 'fall')

$session_condition = array(
    'table' => 'assigned_explorations',
    'alias' => 'AssignedExploration',
    'type' => 'INNER',
    'conditions' => array(
        'AssignedExploration.application_id = Application.id',
        'OR' => array(
            array('AssignedExploration.label' => $this->request->named['filter_session']),
            array('Season.name' => 'fall')
        )
    )
);

但没有运气。似乎季节未被AssignedExploration识别。

我想从AssignedExploration嵌套到ExplorationExplorationCategorySeason

我不知道如何通过cakephp通过inner join查询。

先谢谢。

编辑:截至目前的查询:

INNER JOIN 
    `ntc_development`.`assigned_explorations` AS `AssignedExploration` ON ( 
        `AssignedExploration`.`application_id` = `Application`.`id` 
         AND 
         `AssignedExploration`.`label` = 'fall'
     )

1 个答案:

答案 0 :(得分:0)

$this->paginate['Application'] = array(
                'recursive' => -1,
                'joins' => array(
                    array(
                        'table' => 'assigned_explorations',
                        'alias' => 'AssignedExploration',
                        'type' => 'inner',
                        'foreignKey' => true,
                        'conditions' => array('AssignedExploration.application_id = Application.id')
                    ),
                    array(
                        'table' => 'seasons',
                        'alias' => 'Season',
                        'type' => 'inner',
                        'foreignKey' => true,
                        'conditions' => array('Season.id = Application.season_id') # whats your foregin key
                    ),                                         
                ),
                'fields' => array(),
                'conditions' => array('Season.name' => 'fail'),
            );