1

こんにちは、コントローラーのメソッドを使用してモデルに変更を保存したい場合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'));
            }
        }
    }
4

1 に答える 1

1

edit アクションのコントローラーの名前にスペルミスがあります。

使用する:

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

それ以外の:

$this->Uesr->save($this->request->data)
于 2013-03-29T13:05:08.903 に答える