CakePHP 仅使用连接表使用 HABTM 进行查找

发布于 2025-01-08 02:06:33 字数 1355 浏览 2 评论 0原文

我有一个 HasAndBelongsToMany Users 的目标模型。该 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)
        ));
    }
}

我将参与者绑定为 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

I have a Goal model that HasAndBelongsToMany Users. This HABTM is named Participant. When I try to find all the Participants of a goal, the join table for this HABTM is not being used. Here is the related code in the goal model.

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)
        ));
    }
}

I am binding the Participant as hasOne so that it will create a join in the query, but I get the following error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-01-15 02:06:33

我在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是正确的语法。我非常确定,如果连接表被命名为 goal_participants,正确的语法将是 GoalsParticipant,但由于它被命名为 goal_participants,我猜它可能是 GoalParticipant。

I edited the answer after OP's comment.

If i understand well what you intend to do, this piece of code might help :

function getParticipantIDs($goalID) {
    $participants = $this->GoalParticipant->find('list', array(
        'fields' => array('user_id'),
        'conditions' => array('goal_id' => $goalID)
    ));
    return array_values($participants);
} 

I'm not 100% sure GoalParticipant is the correct syntax. I'm pretty sure that if the join table was named goals_participants, the correct syntax would be GoalsParticipant but as it's named goal_participants I guess it might be GoalParticipant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文