在CakePHP中删除深度关联

在CakePHP中删除深度关联,cakephp,Cakephp,我有一个课程模型,它为我提供了以下阵列: [User] => Array ( [0] => Array ( [id] => 4 [username] => itaita [password] => sdfasdgadgadfgsfdgsdgsdgsdgdsg

我有一个
课程
模型,它为我提供了以下阵列:

[User] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [username] => itaita
                    [password] => sdfasdgadgadfgsfdgsdgsdgsdgdsg
                    [email] => itaita@xyz.com
                    [name] => 
                    [role] => admin
                    [hash_change_password] => 
                    [created] => 2014-03-16 21:06:48
                    [modified] => 2014-03-16 21:06:48
                    [CoursesUser] => Array
                        (
                            [id] => 1
                            [user_id] => 4
                            [course_id] => 1
                        )

                )

        )
我想创建一个订阅/取消订阅按钮,该按钮将删除
CoursesUser
,其中
id=此用户id
course\u id=此课程
,但我不知道如何从课程控制器和视图访问它

目前我有:

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->User->delete(array('CoursesUser.user_id'=> $userid), false)) {
            $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'));
    }
但这会删除用户

我知道问题在这里:
$this->Course->User->delete
,但我不知道如何更改它来处理
courseuser
数组

救命啊

你可以用这个:

$this->Course->User->CoursesUser->deleteAll(数组('CoursesUser.User\u id'=>$User\u id',CoursesUser.courseu id'=>$id),false)


基本上,假设您正确设置了模型关系,这将只删除与您从auth检索到的用户id相匹配的CoursesUser行,这些用户id也提供给操作

您的
诅咒用户
模型是否与
诅咒
相关?我猜你只想删除那个模型中的条目,因为
用户有很多诅咒
对吧?因此,您应该能够执行以下操作:
$this->Curse->curseuser->delete(…)
;我试过这个-但它说的是在非对象上尝试这样做。我认为这是因为我需要以某种方式确定课程可能拥有的众多用户中的->用户。你不必这样做,基本上你只需要从CoursesUser中删除一条记录。您想删除:-具有用户id=您传递的用户id的任何记录。-has course_id=您传递的课程id。可能是你的关系设置不正确,在这种情况下,你能试试这个吗$此->加载模型('CoursesUser')$this->CoursesUser->deleteAll(数组('CoursesUser.user\u id'=>$user\u id,'CoursesUser.course\u id'=>$id),false);看看这能不能解决问题?