我正在使用 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)) {
}
}
}
而模型中的 beforeSave 函数是:
公共函数 beforeSave($options = array()) {
if (isset($this->data['User']['password'])) { $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); } return true;
}
在用户对象中,这个 id=>false 对我来说看起来很可疑
object(User) { displayField => 'firstname' primaryKey => 'id' useDbConfig => 'default' useTable => 'users' id => false data => array( [最大深度达到] ) }