在 CakePHP 2 中编辑 table:Update/Insert 错误

Edit table in CakePHP 2: Update/Insert error

问题:编辑条目时 SQL 正在插入而不是更新。

我有两个 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(); ?>

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

Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

因为 SQL 查询不是更新而是插入。不知道为什么。

SQL Query: INSERT INTO cake.user_details (adress_ex, city_ex) VALUES ('roses street', 'London')

谢谢!

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

您的 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->request->data。应该这样做。就像我说的,检查数据库中的 table id,它必须是自动递增的。

您不能通过在控制器

中告知来更新您的 table
$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,但该模型已存在,因为您未指定要更新它以及要更新哪些列。

希望对您有所帮助

你似乎有几个问题。

$this->request->address 是正确的。表单数据作为 $this->request->data 传递,因此您应该使用 $this->request->data['UserDetail']['address'] etc.

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

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

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

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

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