我有一个HasAndBelongsToMany用户的目标模型。该HABTM被命名为参与者。当我尝试查找目标的所有参与者时,未使用此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)
));
}
}
我将Participant绑定为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
答案 0 :(得分:1)
我在OP发表评论后编辑了答案。
如果我很清楚你打算做什么,这段代码可能会有所帮助:
function getParticipantIDs($goalID) {
$participants = $this->GoalParticipant->find('list', array(
'fields' => array('user_id'),
'conditions' => array('goal_id' => $goalID)
));
return array_values($participants);
}
我不是100%确定GoalParticipant是正确的语法。我很确定如果连接表名为goals_participants,正确的语法将是GoalsParticipant,但是因为它的名字是goal_participants,我想它可能是GoalParticipant。