1
我在cakephp框架上構建了CMS。我只能刪除一個或多個標籤,而不是全部標籤,因此如何從帖子中刪除所有標籤。 我有以下文件。如何刪除CakePHP框架的編輯帖子中的所有標籤?
郵政型號:
<?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' => '',
)
);
>
標籤型號:??
<?php
public $displayField = 'name';
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Post' => array(
'className' => 'Post',
'joinTable' => 'posts_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'post_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
>
柱控制器:
<?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);
}
}
}
}
?
>
admin_edit.ctp:
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'));
謝謝你的幫助。