1
我有一個HasAndBelongsToMany用戶的目標模型。這個HABTM被命名爲參與者。當我嘗試查找目標的所有參與者時,此HABTM的連接表未被使用。以下是目標模型中的相關代碼。CakePHP僅使用連接表與HABTM查找
class Goal extends AppModel {
var $hasAndBelongsToMany = array(
'Participant' => array(
'className' => 'User',
'joinTable' => 'goal_participants',
'foreignKey' => 'goal_id',
'associationForeignKey' => 'user_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
));
function getParticipantIDs($goalID) {
$this->bindModel(array('hasOne' => array('Participant')));
return $this->find('list', array(
'fields' => array('Participant.user_id'),
'conditions' => array('Participant.goal_id' => $goalID)
));
}
}
我綁定的參與者爲hasOne,這樣它會創建在查詢中加入,但我得到以下錯誤:
Warning (512): SQL Error: 1054: Unknown column 'Participant.user_id' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 525]
Query: SELECT `Participant`.`id`, `Participant`.`user_id` FROM `users` AS `Participant` WHERE `Participant`.`goal_id` = '19' AND `Participant`.`status` != 2
感謝您的回答,G.J.不幸的是,它不工作,我有一種感覺,我沒有解釋我的模式。目標包含'id'字段,參與者(又名用戶)也是如此。連接它們的表被稱爲「goal_participants」,其中包含「goal_id」和「user_id」字段。我正在尋找的是'goal_participants'表中具有指定'$ goalID'的行。您提供的查詢是在Users表中尋找'goal_id'(當它真的在'goal_participants'表中)時。任何想法如何從連接表中獲取數據? – Garrett 2012-02-23 20:30:34
我的不好。我編輯了我的答案。我認爲現在應該是正確的 – 2012-02-24 01:46:15