我正在开发一个使用Docrtrine和Symfony 2.7的项目.我有一个我想要克隆的Document实体,我当然需要确保我没有重复的主键.这是我到目前为止所尝试的:
/**
* Document
*
* @ORM\Table(name="documents")
*/
class Document {
public function ___clone(){
$newObj = clone $this;
$newObj->id = null;
return $newObj;
}
...
}
但是,这似乎并不多,因为当我调用clone myDocument然后尝试持久化时,我仍然收到此消息:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
‘1’ for key ‘UNIQ_A2B07288ECC6147F’
如何让对象的主键恢复为空或自动递增状态?
=====
Upate:使用
public function __clone(){
$this->id = null;
}
仍会导致相同的错误.完整错误文字:
An exception occurred while executing ‘INSERT INTO documents
(usageFrom, usageTo, status, workflow_identifier, created_date,
modified_date, language_id, translationRoot_id, ownerGroup_id,
responsibleUser_id, production_id, media_id, created_user,
modified_user) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)’ with
params [“2018-06-28 09:54:37”, “2018-06-28 09:54:37”, 100,
“4cc723c2a5730c1b9c2ed6428ae57205”, “2018-06-28 09:54:37”, “2018-06-28
09:54:37”, null, null, null, null, 1, null, 1, 1]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
‘1’ for key ‘UNIQ_A2B07288ECC6147F’