Cakephp - 加入表,而不是

时间:2014-02-15 17:38:14

标签: cakephp

我是一个新手,我正在尝试创建一个查询但是没有得到它如何以蛋糕式进行查询。我想做一个查询选择所有不属于特定转型的受访者。底部的例子。 Cakephp v 2.4.5

我有三张桌子/模型:

Resondents:id,name

public $hasAndBelongsToMany = array(
    'Transformational' => array(
        'className' => 'Transformational',
        'joinTable' => 'transformationals_respondents',
        'foreignKey' => 'respondent_id',
        'associationForeignKey' => 'transformational_id',
        'unique' => true
    )
);

转化:id,name

   public $hasAndBelongsToMany = array(
    'Respondent' => array(
            'className' => 'Respondent',
        'joinTable' => 'transformationals_respondents',
        'foreignKey' => 'transformational_id',
        'associationForeignKey' => 'respondent_id',
        'unique' => true
        ),
};

TransformationasRespondent:id,transformational_id,respondent_id     这是一个连接表

示例表格: 受访者:身份证,姓名 1,Abc 2,Def 3,Ghi 4,Jkl

转型:id,name 1,Macrosoft 2,艾迪 3,Wag

transformationals_respondents:id,respondent_id,transformational_id 1,1,7 2,2,7

我需要查询来选择那些不在transformationals_respondents中并且具有transformational_id 7的受访者。即。受访者Ghi和Jkl

我真的很感激这里的一只手。

1 个答案:

答案 0 :(得分:0)

在Respondent模型中创建一个函数

function getSomeSome(){

$options = array(
        'conditions' => array('Transformational.id'=>7,'TransformationasRespondent.id IS NULL'),
        'joins' => array(
            array(
                'alias' => 'TransformationasRespondent',  
                'table' => 'transformationals_respondents',
                'type' => 'LEFT',
                'conditions' => array(
                    'TransformationasRespondent.respondent_id = Respondent.id',
                ),
            ),
            array(
                'alias' => 'Transformational',  
                'table' => 'transformationals',
                'type' => 'LEFT',
                'conditions' => array(
                    'TransformationasRespondent.transformational_id = Transformational.id',
                ),
            ),

        )
    );
  $returnData = $this->find('all',$options);
  # returnData contains all the records having transformational_id equals to 7 and
  # does nt have any recors in TransformationasRespondent table
}