如果我想通过控制器的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'));
}
}
}
答案 0 :(得分:1)
编辑操作中控制器名称存在拼写错误。
使用:
$this->User->save($this->request->data)
而不是:
$this->Uesr->save($this->request->data)