用于删除CakePHP中的hasMany Through记录的“Where”语句

时间:2014-03-17 15:28:25

标签: php cakephp

我与CourseUserCourseMembership

进行了多次合作

我正在尝试在课程视图中创建取消订阅按钮。目前这是我拥有的数组:

(
    [Course] => Array
        (
            [id] => 1
            [name] => Flying
            [description] => Oh! The Magic!
            [created] => 2014-03-05 14:45:21
            [modified] => 2014-03-05 14:45:21
        )

    [Lesson] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [title] => Growing Wings
                    [description] => Here's how you grow wings!
                    [course_id] => 1
                    [created] => 2014-03-05 14:19:38
                    [modified] => 2014-03-05 14:19:38
                )

            [1] => Array
                (
                    [id] => 2
                    [title] => Taking Flight
                    [description] => You are finally ready to take flight.
                    [course_id] => 1
                    [created] => 2014-03-06 11:49:51
                    [modified] => 2014-03-06 11:49:51
                )

        )

    [CourseMembership] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [user_id] => 4
                    [course_id] => 1
                )

        )

)

我设置了一个带有删除操作的CourseMemberships控制器。 但是我在课程中设置模型时遇到问题,以区分CourseMembership的相应ID,其中user_id是登录用户(CakeSession::read("Auth.User.id");),课程是当前课程。

修改 这是课程unsubscribe()控制器:

public function unsubscribe($id = null) {
        $this->Course->id = $id;
        $userid = CakeSession::read("Auth.User.id");

        if (!$this->Course->exists()) {
            throw new NotFoundException(__('Invalid course'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Course->CourseMembership->delete(array('CourseMembership.user_id'=> $userid))) {
            $this->Session->setFlash(__('The course has been deleted.'));
        } else {
            $this->Session->setFlash(__('The course could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }

修改 这是我正在创建取消订阅的链接。

            <?php echo $this->Form->postLink(__('Unsubscribe'), array('controller' => 'CourseMemberships', 'action' => 'unsubscribe', $course['Course']['id'], CakeSession::read("Auth.User.id")), null, __('Are you sure you want to delete # %s?', $course['Course']['id'])); ?>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望删除对登录用户的特定课程的订阅。因此,在课程控制器中的取消订阅操作中,您可能会遇到以下情况:

// 课程控制器

public function unsubscribe($id = null){
    if (!$this->request->is('post')) {
        throw new MethodNotAllowedException();
    }
    $this->Course->id = $id;
    if (!$this->Course->exists()) {
        throw new NotFoundException(__d('Course', 'Invalid course'));
    }
    if($this->Course->CourseMembership->unsubscribe($id, CakeSession::read('Auth.User.id'))){
        $this->Session->setFlash(__d('Course', 'Unsubscribed!'));
        $this->redirect(array('controller' => 'courses', 'action' => 'go_here_etc'));
    }
    $this->Session->setFlash(__d('Course', 'Attention! Not Unsubscribed!'));
    $this->redirect(array('controller' => 'courses', 'action' => 'go_here_etc'));
}

// 课程成员模型

public function unsubscribe($courseId, $userId){
    // I assume that each user can subscribe only one time the same course. This calls the ids inputted in the unsubscribe link.
    return $this->deleteAll(array('CourseMembership.course_id' => $courseId, 'CourseMembership.user_id' => $userId));
}