我正在尝试从cakephp 3中的http post请求中创建一个新的数据库条目。在cakephp 3中,他们现在正在使用实体来操纵数据。我有一个使用json的休息api。我想把这个json数据放到我的数据库中,这就是我在做什么 -
//在控制器中
public function add()
{
$res = array();
$book = $this->Books->newEntity();
if ($this->request->is('post'))
{
$book = $this->Books->patchEntity($book, $this->request->data);
if ($this->Books->save($book))
{
$res['status'] = 1;
$res['msg'] = 'The book has been saved.';
} else {
$res['status'] = 0;
$res['msg'] = 'The book could not be saved. Please, try again.';
}
}
$this->set(compact('res'));
$this->set('_serialize', ['res']);
}
//输出“http://localhost/MyApp/books/add.json” - >
{
“res”:{ “状态”:0, “msg”:“这本书无法保存。请再试一次。” } }
数据未保存在数据库中。