0
votes

Hello I have done something similar in PHP core, but trying to do the same in cakephp, is proving difficult for me. I want to take the tags input, and explode the POST data and insert the the new array into its own table using the posts id for each tag separated by a ",". However when I insert the exploded array is empty when the post is created.

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

1 Answers

1
votes

$this->set() is for sending variables to the view in CakePHP. You don't need it to retrieve data.

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