1

I am trying to do a new database entry off of a http post request in cakephp 3. In cakephp 3 they are now using entities to manipulate the data. I have a rest api that uses json. I want to take that json data and put it in my database, here is what I am doing -

// In controller

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']);
} 

// Output on "http://localhost/MyApp/books/add.json" -->

{

"res": { "status": 0, "msg": "The book could not be saved. Please, try again." } }

Data not save in database.

4

0 回答 0