itamar
itamar

Reputation: 3967

A "Where" statement for deleting a hasMany Through record in CakePHP

I have a hasMany through with Course, User, and CourseMembership

I am trying to create an unsubscribe button on the Courses view. At the moment this is the array I have:

(
    [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
                )

        )

)

I set up a CourseMemberships controller with a delete action. But I am having trouble setting up the model in Courses to distinguish the appropriate ID of the CourseMembership where the user_id is the logged in user (CakeSession::read("Auth.User.id");) and the course is the current course.

Edit: Here's the Courses unsubscribe() controller:

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

Edit: Here's the link I'm creating to unsubscribe.

            <?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'])); ?>

Upvotes: 0

Views: 90

Answers (1)

manzapanza
manzapanza

Reputation: 6215

If I understand correctly, you want to delete the subscription to a specific course of the user logged in. So in your unsubscribe action in the Courses controller you may be have something like this:

// Courses Controller

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

//CourseMembership Model

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

Upvotes: 1

Related Questions