【发布时间】:2013-07-10 15:42:13
【问题描述】:
我正在使用 CakePHP v2.3.5。当我更新表格时
`users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(320) NOT NULL,
`password` varchar(45) NOT NULL,
`firstname` varchar(255) DEFAULT NULL,
`lastname` varchar(255) DEFAULT NULL,
`country` varchar(255) DEFAULT 'SG',
PRIMARY KEY (`id`)
);
使用$this->User->save($this->request->data),它总是触发插入命令并抛出
"Integrity constraint violation: 1062 Duplicate entry '10' for key 'PRIMARY'"
request->data 对象是:
array(
'User' => array(
'id' => '10',
'firstname' => 'Someone',
'lastname' => 'Surname',
)
)
我现在使用的完整操作是:
public function addInfo() {
if ($this->request->is('post')) {
$this->User->create();
$this->User->id=(int)$this->request->data['User']['id'];
if ($this->User->save($this->request->data)) {
}
}
}
而Model中的beforeSave函数是:
公共函数 beforeSave($options = array()) {
if (isset($this->data['User']['password'])) { $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); } return true;}
在用户对象中,这个 id=>false 在我看来很可疑
对象(用户){ 显示字段 => '名字' 主键 => 'id' useDbConfig => '默认' useTable => '用户' id => 假 数据 => 数组( [达到最大深度] ) }
【问题讨论】:
-
尝试在
request->data数组中使用'id'=>10(10 作为整数) -
感谢您编辑帖子。将 id 转换为整数,'User' => array('id' => (int) 10,'firstname' => 'Someone','lastname' => 'Surname') 但问题仍然存在。
-
请显示您的整个控制器操作代码。仅仅引用您正在使用的保存方法并不能帮助我们帮助您。
-
如果在保存操作之前硬编码
$this->User->id = 10;会发生什么? -
我硬编码了 $this->User->id,还是一样。然后我尝试硬编码用户 ID,然后是 unset($this->request->data['User']['id']),它作为新行插入。
标签: cakephp cakephp-2.3