dtf1111 2013-10-21 11:55
浏览 50
已采纳

Cakephp更新密码

I am trying to edit one of my members password (i have been allowed by the user).

Now for this function i have the following action:

        if (!$this->User->exists($id)) {
        throw new NotFoundException(__('Invalid USer'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        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.'));
        }
    } else {
    }
}

However when i try to save i get the following error:

2013-10-21 11:53:53 Error: [PDOException] SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'MarcEmp' for key 'username'

So it tries to insert a new entry.

Does anyone know what i am doing wrong?

  • 写回答

1条回答 默认 最新

  • dongqieli4164 2013-10-21 11:57
    关注

    You are forgetting to set the current id to update, so Cake will try to create a new record by default. Try adding:

    $this->User->id = $id;
    

    Just before the save operation. So your entire function should look like:

    /**
     * Edit an existing user.
     *
     * @param int $id The user id to edit.
     */
    public function edit($id) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid User'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->User->id = $id; // <-- Add it here
            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.'));
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题