こんにちは、コントローラーのメソッドを使用してモデルに変更を保存したい場合edit()
、次のエラーが発生します。
Error: Call to a member function save() on a non-object
File: K:\xampp\xampp\htdocs\app\Controller\UsersController.php
Line: 39
これは方法です:
public function edit($id = null){
$this->User->id = $id;
if(!$this->User->exists()){
throw new NotFoundException(__('Invalid user'));
}
if($this->request->is('post')){
###### line number 39 - Where error happens >>>>
if($this->Uesr->save($this->request->data)){
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__('The user could not be saved. Please, try again'));
}
}else{
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
しかし、add()
メソッド内でこのオブジェクトを呼び出すと、エラーは発生しません。これはadd()
メソッドのコードです:
public function add(){
if($this->request->is('post')){
$this->User->create();
if($this->User->save($this->request->data)){
$this->Session->setFlash(__('The user has been saved.'));
$this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__('The user could not be saved. Please, try again'));
}
}
}