I'm attempting to update records through decoded JSON by ID in this CakePHP function:
public function update() {
$this->layout = 'ajax';
if($this->request->is('post')) {
$decoded = json_decode($this->request->data,true);
if($data = $this->Foobar->save($decoded)) {
$data = json_encode(array(
"message" => "Foobar successfully updated.",
"update" => $this->request->data
));
} else {
$data = json_encode(array(
"message" => "Foobar could not be updated.",
"update" => $decoded,
"updateJson" => $this->request->data
));
}
} else {
$data = json_encode(array(
"message" => "Method should be post."
));
}
$this->set('data', $data);
But the decode keeps returning null
:
{"message":"Foobar could not be updated.","update":null,"updateJson":{"ID":"1","status":2}}
However, if I go to http://www.compileonline.com/execute_php_online.php and enter:
<html><head></head><body>
<pre>
<?php
print_r(json_decode('{"ID":"1","status":2}', true));
?>
</pre>
</body></html>
It works just fine...
Looking at related questions...
json_last_error()
, this returns 0
for me.magic_quotes
might be on, mine are off.json_decode(utf8_encode($this->request->data),true);
, this still returns null
for meAny ideas?
I don't think you should JSON Decode the data.
Try to directly save:
$data = $this->Foobar->save($this->request->data);