我使用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)
表,它總是觸發一個INSERT命令,並拋出
"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)) {
}
}
}
,並在型號的beforeSave功能是:
公共函數beforeSave($選項=陣列()){
if (isset($this->data['User']['password'])) { $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); } return true;
}
在用戶對象,此ID =>假看起來很腥
object(User){ displayField > '姓名' 的PrimaryKey =>的 'id' useDbConfig => '缺省' useTable => '用戶' ID =>假 數據=>數組( [最大深度達到] ) }
嘗試在'request-> data'數組中使用''id'=> 10'(10爲整數) – Nunser
感謝您編輯帖子。將id作爲整數'User'=> array('id'=>(int)10,'firstname'=>'Someone','lastname'=>'姓氏'),但問題仍然存在。 – user2078719
請顯示您的整個控制器操作代碼。只是引用您正在使用的保存方法不會幫助我們幫助您。 – Oldskool