您好我在PHP核心中做过类似的事情,但是尝试在cakephp中做同样的事情对我来说很难。我想获取标签输入,并爆炸POST数据,并使用由“,”分隔的每个标签的posts id将新数组插入到自己的表中。但是,当我在创建帖子时插入爆炸数组为空时。
add.ctp
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->input('tags', array('label' =>'Separate tags by commas'));
echo $this->Form->end('Save Post');
PostsController
public function add() {
if ($this->request->is('post')) {
$tags = $this->set($this->request->data['Post']['tags']);
$exploded_tags = explode(",",$tags);
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
答案 0 :(得分:1)
$this->set()
用于将变量发送到CakePHP中的视图。您不需要它来检索数据。
public function add() {
if ($this->request->is('post')) {
$tags = $this->request->data['Post']['tags'];
$exploded_tags = explode(",",$tags);
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}