【问题标题】:cakephp's model save method doesnt workcakephp模型保存方法不起作用
【发布时间】: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

标签: php cakephp


【解决方案1】:

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

用途:

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

代替:

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

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多