0

It's a hard one to explain as my code half works.

Basically this is what I have:

public function add() {
    $this->set('branchingQuestions', $this->BranchingQuestion->find('all'));

    if ($this->request->is('post')) {
        $_POST['data']['Job']['branchingQuestions'] = mysql_escape_string(serialize($_POST['data']['Job']['branchingQuestions']));
        var_dump($_POST['data']['Job']);
        //die;
        $this->Job->create();
        if ($this->Job->save($this->request->data)) {
            $this->Session->setFlash(__('The job has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The job could not be saved. Please, try again.'));
        }
    }
}

This is in a Cake PHP Controller. Most of that code has been generated through Bake.

Anyway in my POST array is another array under the name of branchingQuestions and because its an array I get an error when inserting as you'd expect. However from the var_dump I get that the array has successfully serialized and is now a string just the same as the rest of the contents of the POST is. So it should work.

However from testing a couple of things it seems that CakePHP doesn't even realise that I have updated the array with the serialize line. It runs as if that never happened even though my var_Dump suggests otherwise.

['data']['job'] contains all my strings and int's such as ['data']['job']['title'] and they get inserted fine, but to test my theory I also changed ['title'] like this:

$_POST['data']['Job']['title'] = 120;

Which should of made SQL give an error as title is a string. But it didn't even change title was still "test" in my table.

branchingQuestions is posted like so:

<select name='data[Job][branchingQuestions][question" + branchCount + "]'

I have a dynamic form that you can add more select boxes to when you need, thats why I have a variable in the question part. All this works ok, the var_dump gives me what I expected.

1 Answer 1

1

You actually have to edit $this->request->data array instead of the $_POST array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it worked. I didn't realise that there was a difference between using $_POST and $this->request->data. As I haven't been using html helpers in the view but that works fine so I just assumed. Anyway thanks again, the obvious things always allude me.

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.