0

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 Answer 1

1

$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.'));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

ok I see, so I have to use the $this->post then the form elements, I was told in cake there is a easy way to use saveall() to insert a new post, but also save that post's id to then insert into another table, all within the same expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.