【问题标题】:Edit table in CakePHP 2: Update/Insert error在 CakePHP 2 中编辑表:更新/插入错误
【发布时间】:2015-11-20 10:00:39
【问题描述】:

问题:在编辑条目时,SQL 正在执行 INSERT 而不是 UPDATE。

我有两个 SQL 表:users 和 users_detail。 users(id,role,name,password) & users_detail(id,adress,city,user_id)

外键users_detail.user_id 链接到users.id

我的 cakephp 应用程序中有一个表单来编辑 users_detail,例如,如果用户想要编辑他的地址。 这是我的控制器:

        public function admin_edit_dashboard($id = null){
    if (!$this->User->exists($id)) {
        throw new NotFoundException('Invalid user details');
    }
    if ($this->request->is('post') || $this->request->is('put')) {

        if ($this->User->UserDetail->save($this->request->data, true, ['id', 'address', 'city'])) {
            $this->Session->setFlash('The user has been saved');
            return $this->redirect(array('action' => 'dashboard'));
        } else {
            $this->Session->setFlash('The user could not be saved. Please, try again.');
        }
    } else {
        //$this->request->data = $this->User->read(null, $id);
        $user = $this->User->UserDetail->find('first', array(
            'conditions' => array(
                'UserDetail.user_id' => $id
            )
        ));
        $this->request->data = $user;
    }
    $this->set(compact('user'));
}

还有我的表格:

<?php echo $this->Form->create('UserDetail');?>
        <?php echo $this->Form->input('address', array('class' => 'form-control')); ?>
        <br />
        <?php echo $this->Form->input('city', array('class' => 'form-control')); ?>
        <br />
        <?php echo $this->Form->button('Submit', array('class' => 'btn btn-primary')); ?>
        <?php echo $this->Form->end(); ?>

但是当我验证表单以编辑详细信息时出现错误:

错误:SQLSTATE[23000]:违反完整性约束:1062 键“PRIMARY”的重复条目“0”

因为 SQL 查询不是 UPDATE 而是 INSERT。我不知道为什么。

SQL 查询:INSERT INTO cake.user_details (adress_ex, city_ex) 价值观 ('玫瑰街', '伦敦')

谢谢!

编辑:它有效,感谢您的帮助!添加了很好的代码。

【问题讨论】:

  • admin_edit_dashboard 在哪个控制器中?

标签: php cakephp cakephp-2.0


【解决方案1】:

您似乎有几个问题。

$this-&gt;request-&gt;address 是正确的。表单数据以$this-&gt;request-&gt;data 传递,因此您应该使用$this-&gt;request-&gt;data['UserDetail']['address'] etc

如果您想确保只保存特定字段,您可以将它们作为save() 的第三个参数传递,然后您不需要事先使用set():-

$this->UserDetail->save($this->request->data, true, ['id', 'address', 'city']);

SQL 错误意味着您在user_details 表上的主键未设置为自动递增(应该如此)。这就是INSERT 失败的原因。

您还忘记传递您要更新的记录的主键,因此 Cake 假设您要创建一条新记录。确保包含此内容,以便 Cake 知道使用 UPDATE。例如,在您的视图表单中包含以下内容:-

echo $this->Form->hidden('id');

【讨论】:

  • 感谢您的所有建议,现在可以使用了!我将编辑我的第一个以显示正确的代码。完美结合天行者的建议。
【解决方案2】:

您的User 模型应该与UserDetail 有关联,例如:

public $hasOne = array('UserDetail');

还有你的UserDetail:

public $belongsTo = array('User');

那么你不必使用loadModel,只需:

$this->User->UserDetail->find('first', array(...));

您应该通过UserDetailsController 操作编辑用户详细信息。

在您的视图中添加行以了解您正在编辑的内容:

echo $this->Form->input('id');

然后只需使用传递的$this-&gt;request-&gt;data 保存即可。那应该这样做。就像我说的,检查数据库中的表ID,它必须是自动增量的。

【讨论】:

  • 感谢您的建议,我已经更新了我的代码和我的第一篇文章。
  • 感谢您的建议,drmonkeyninja 的解决方案对我帮助很大!
【解决方案3】:

您不能通过在控制器中告知来更新您的表格

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

相反,您应该尝试做类似的事情

//find that specific row in database and then update columns and save it

$this->UserDetail->set(array('address'=>$request->address,'city'=>$request->city));                
$this->UserDetail->save();

您的模型希望将其保存为 id = 0 下已经存在的新 user_details,因为您没有指定要更新它以及要更新哪些列。

希望对你有帮助

【讨论】:

  • 谢谢,有帮助!但现在我的字段被检测为空白。它们未经验证。我不知道为什么。我已经更新了我的帖子,向您展示我的新代码。感谢您的帮助。
  • 现在试试,我已经更新了我的代码,因为我不知道表单中输入字段的“名称”
  • 感谢您的建议,对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
相关资源
最近更新 更多