2013-01-01 42 views
1

嗨,當我想通過控制器的edit()方法將更改保存到我的模型下面的錯誤發生:CakePHP的模型保存方法不工作

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')); 
      } 
     } 
    } 
+7

你拼寫的型號名稱爲 'UESR' 上的錯誤正在發生的事情就行了。這是你的問題嗎? – Josh

+1

不,這是正確的 –

+1

@MustafaShujaie仔細檢查語法,你在哪裏啓動了一個'Uesr'模型?請不要指出s和e:'User' - >'Uesr'。看起來像我的錯字。 OMG, – Oldskool

回答

1

編輯操作中控制器名稱中存在拼寫錯誤。

用途:代替

$this->User->save($this->request->data) 

$this->Uesr->save($this->request->data)