【发布时间】: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