Reputation: 5
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.
Upvotes: 0
Views: 1483
Reputation: 736
You actually have to edit $this->request->data array instead of the $_POST array.
Upvotes: 1