Mysql 在cakephp2.4.0中更新用户数据时出错

Mysql 在cakephp2.4.0中更新用户数据时出错,mysql,cakephp,Mysql,Cakephp,保存编辑的数据时出错。实际上,当用户想要保存编辑后的数据时,点击编辑按钮的用户会在最后(编辑后)重定向到编辑数据页cakephp给出错误sql完整性冲突代码1062。编辑代码是蛋糕烘焙生成的默认代码。代码是 public function edit($id = null) { if (!$this->User->exists($id)) { throw new NotFoundException(__('Invalid user')); } i

保存编辑的数据时出错。实际上,当用户想要保存编辑后的数据时,点击编辑按钮的用户会在最后(编辑后)重定向到编辑数据页cakephp给出错误sql完整性冲突代码1062。编辑代码是蛋糕烘焙生成的默认代码。代码是

public function edit($id = null) {
    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.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    } else {
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->request->data = $this->User->find('first', $options);
    }
}

我还尝试了savefield而不是save,但这是在添加具有所有空字段的新用户。

在保存用户之前,您应该设置该用户的id属性:

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

或者确保在您的
$this->request->data中
,您正在编辑的对象的id存在,
$this->request->data['User']['id']
在这种特殊情况下,请求数据上缺少用户id是导致问题的原因。

也请输入查看文件代码!!你能不能发布cake创建的插入查询给你这样一个错误?事实上,视图文件是空的ctp文件,因为我使用它作为android phone的rest服务。你可能忘记了为主键字段“id”设置“autoincrement”。