删除HABTM关系的连接表中的记录

时间:2013-06-22 17:34:06

标签: cakephp has-and-belongs-to-many

这种关系看起来像这样:

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

UserController.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')));

我看了description here,,但对我来说并不清楚。 请帮我一个例子或更正,非常感谢!

0 个答案:

没有答案