如何删除cakephp framwork编辑帖子中的所有标签?

如何删除cakephp framwork编辑帖子中的所有标签?,cakephp,cakephp-2.1,Cakephp,Cakephp 2.1,我已经在cakephp框架上构建了CMS。我可以只删除一个或多个标签,而不是全部,那么我如何从帖子中删除所有标签呢。 我有以下文件 后模型: <?php public $displayField = 'title'; public $hasAndBelongsToMany = array( 'Tag' => array( 'className' => 'Tag', 'joinTable' => 'posts_tags',

我已经在cakephp框架上构建了CMS。我可以只删除一个或多个标签,而不是全部,那么我如何从帖子中删除所有标签呢。 我有以下文件

后模型:

<?php
public $displayField = 'title';

public $hasAndBelongsToMany = array(
    'Tag' => array(
        'className' => 'Tag',
        'joinTable' => 'posts_tags',
        'foreignKey' => 'post_id',
        'associationForeignKey' => 'tag_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => '',
    )
);
谢谢您的帮助。

这可能会帮助您:

使用deleteAll函数,您可以传递Posts id并删除与其相关的所有标记

$conditions = array('Tag.id' => 1234);
$this->Post->Tag->deleteAll($conditions, false);
<?php
public function admin_edit($id = null) {
    $this->Post->id = $id;
    if (!$this->Post->exists()) {
        throw new NotFoundException(__('Invalid post'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {

        $tags = explode(',', $this->request->data['Post']['tags']);
        foreach ($tags as $_tag) {
            $_tag = strtolower(trim($_tag));
            if ($_tag) {
                $this->Post->Tag->recursive = -1;
                $tag = $this->Post->Tag->findByName($_tag);
                if (!$tag) {
                    $this->Post->Tag->create();
                    $tag = $this->Post->Tag->save(array('name' => $_tag, 'slug' => $_tag));
                    $tag['Tag']['id'] = $this->Post->Tag->id;
                    if (!$tag) {
                        $this->_flash(__(sprintf('The Tag %s could not be saved.', $_tag), true), 'success');
                    }
                }
                if ($tag) {
                    $this->request->data['Tag']['Tag'][$tag['Tag']['id']] = $tag['Tag']['id'];
                }
            }
        }

        if ($this->Post->save($this->request->data)) {

            $this->Session->setFlash(__('The post has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The post could not be saved. Please, try again.'));
        }
    } else {
        if (empty($this->request->data)) {
            $this->request->data = $this->Post->read(null, $id);
            $tags = array();
            if (isset($this->request->data['Tag']) && !empty($this->request->data['Tag'])) {
                foreach ($this->request->data['Tag'] as $tag) {
                    $tags[] = $tag['name'];
                }
                $this->request->data['Post']['tags'] = implode(', ', $tags);
            }
        }
    }
}
echo $this->Form->create('Post');
    echo __('Admin Edit Post');
    echo $this->Form->input('id');
    echo $this->Form->input('user_id');
    echo $this->Form->input('title');
    echo $this->Form->input('slug');
    echo $this->Form->input('content');
    echo $this->Form->input('excerpt');
    echo $this->Form->input('status');
    echo $this->Form->input('comment_status');
    echo $this->Form->input('comment_count');
    echo $this->Form->input('type');
    echo $this->Form->input('Category');
    echo $this->Form->input('Post.tags');
         echo $this->Form->end(__('Submit'));
$conditions = array('Tag.id' => 1234);
$this->Post->Tag->deleteAll($conditions, false);