【发布时间】:2013-03-29 21:28:13
【问题描述】:
您好,当我想通过控制器的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'));
}
}
}
【问题讨论】:
-
您在错误发生的那一行将型号名称拼写为“Uesr”。这是你的问题吗?
-
@MustafaShujaie 仔细检查该语法,您在哪里启动了
Uesr模型?请注意 s 和 e 切换:User->Uesr。对我来说似乎是一个错字。 -
哦,天哪。我真的很傻很抱歉,谢谢@Josh和Oldskool