cakephp - Add data to store in cakephp3 controller -
i using cakephp-3.0 have problem in version of cakephp 2.x can add data in controller , don't need send view.
example:
public function add() { $this->request->data['user']['id']=$this->auth->user('id');//and save id $this->request->data['user']['date']=$date('y-m-d h:m:s');// , save date if ($this->request->is('post')) { if ($this->users->save($user)) { $this->flash->success('the user has been saved.'); return $this->redirect(['action' => 'index']); } else { $this->flash->error('the user not saved. please, try again.'); } } $this->set(compact('user')); $this->set('_serialize', ['user']); }
up here, add data received, receive information , data added id , date example. example. , if save information, information saved id , date. id , date not in view.
now want similar in cakephp 3 dont work.
public function add() { $user = $this->users->newentity(); $this->request->data['user']['id']=$this->auth->user('id');//and don't save id $this->request->data['user']['date']=$date('y-m-d h:m:s');// , don't save date if ($this->request->is('post')) { $this->request['date'] = date("d-m-y h:i:s"); $user = $this->users->patchentity($user, $this->request->data); debug($user); if ($this->users->save($user)) { $this->flash->success('the user has been saved.'); return $this->redirect(['action' => 'index']); } else { $this->flash->error('the user not saved. please, try again.'); } } $this->set(compact('user')); $this->set('_serialize', ['user']); }
search documentation , found explain more clearly
old:
$this->request->data['user']['id']=$this->auth->user('id');//and don't save id $this->request->data['user']['date']=$date('y-m-d h:m:s');// , don't save date
new:
$user = $this->users->patchentity($user, $this->request->data); $user->id=$this->auth->user('id'); $user->date=$date('y-m-d h:m:s');