php如何禁止克隆,如何取消设置克隆的PHP对象的id

我正在开发一个使用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’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值