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

您在编辑操作中的控制器名称有拼写错误。

利用:

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

代替:

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