0

关系如下所示:

Podcasts <-HABTM-> Users

如何删除它们之间的关联(插入工作正常)?我搜索了很多线程,但我找不到适合我的答案。这是我已经拥有的:

模型/PodcastsUser.php

public function removePodcast($podcastId, $userId){
    if(empty($podcastId) || empty($userId)) return false;

    $this->User->PodcastsUser->deleteAll(array(
        'PodcastsUser.podcast_id' => $this->data['Podcast'][$podcastId], 
        'PodcastsUser.user_id' => $this->data['Podcast'][$userId]
    ));
}

用户控制器.php

public function removepodcast($podcastId, $userId){
    //$this->loadModel('PodcastsUser');
    $this->User = ClassRegistry::init('PodcastsUser');

    if ($this->User->removePodcast($podcastId, $userId)){
        $this->Session->setFlash(__('The podcast has been removed'));
        $this->redirect(array('controller' => 'users', 'action' => 'index'));
    }else {
            $this->Session->setFlash(__('The podcast could not be removed. Please, try again.'));
        }
}

查看/用户/

echo $this->Html->link(__('Remove'), array('controller' => 'users', 'action' => 'removepodcast', $mypodcast['Podcast']['id'], $this->Session->read('Auth.User.id')));

我在这里查看了此描述,但对我来说并不清楚。请帮我举个例子或更正,将不胜感激!

4

0 回答 0