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 -
public function add() {
$user = $this->Users->newEntity();
$user = $this->Users->patchEntity($user, $this->request->data);
$array = $user->toArray();
$this->Users->save($user);}
My json data that is being sent in http post -
{"userID":60,"firstname":"Sherbert60","lastname":"Dafflemore60","profilepic":"nada"}
The error I receive -
Error: Call to a member function errors() on array File /var/app/current/vendor/cakephp/cakephp/src/ORM/Table.php Line: 1356
Does anyone know what I am doing wrong? Thanks
EDIT -
Read in the docs that cakephp will automatically handle it if it is json, so I changed my code to -
$user = $this->Users->newEntity($this->request->data);
if ($this->Users->save($user)) {
$message = 'Saved';
} else {
$message = 'Error';
}
Now I get a 200 response with no entry into database. I can query the database with no problems.