2014-02-15 39 views
-1

我是一個新手,我試圖做一個查詢,但沒有得到它如何做蛋糕式的查詢。我想做一個查詢,選擇所有不屬於特定轉換的受訪者。底部的例子。 CakePHP的v 2.4.5Cakephp - 加入表格,不在

我有三個表/型號:

Resondents:ID,名稱

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

Transformationals:ID,名稱

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

TransformationasRespondent:ID,transformational_id ,respondent_id 這是一個連接表

頁實施例的表: 申請人:ID,名稱 1,美國廣播公司 2,防守 3,GHI 4,JKL

transformationals:ID,名稱 1,Macrosoft 2,渦 3中,WAG

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

然後我需要查詢來選擇那不是受訪transformationals_respondents並且具有TRANSFO rmational_id 7.即。被訪者Ghi和Jkl

我真的很感激這裏的一隻手。

+0

要麼問這是一個MySQL的問題,或者包括你希望建立與蛋糕的實際查詢。通過列出兩個相同的問題,它會出現「你能爲我建立這個」嗎? – Dave

+0

這是離題。我無法看到在MySql部分提出這個問題的原因,它是一個Cake問題。在MySql中進行這個查詢不是一個問題。有誰知道另一個論壇提出這樣的問題,這個論壇耗費了很多精力。我不能看到這個問題,你可以把這個給我。對於一個人來說,這是一個相當簡單的問題。我在論壇上的另一個主題中討論了同樣的問題,並解釋了這個主題。根本沒有反應。爲什麼要花大力氣向不懂如何回答的人解釋問題? – user2895699

+0

你不明白。如果這不是問題,那麼請提供您想要轉換的MySQL查詢,我很樂意提供幫助 - 但是您不應該要求我們爲您寫入查詢,然後將其轉換爲CakePHP。理解? – Dave

回答

0

在答辯模型創建一個功能

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 
}