简体   繁体   English

如何取消设置克隆的 PHP 对象的 id

[英]How to unset the id of a cloned PHP object

I am working on a project that uses Docrtrine and Symfony 2.7.我正在开发一个使用 Doctrine 和 Symfony 2.7 的项目。 I have a Document entity that I want to clone, and I of course need to make sure that I don't have a duplicate primary key.我有一个想要克隆的 Document 实体,我当然需要确保我没有重复的主键。 Here is what I have attempted so far:这是我迄今为止尝试过的:

/**
 * Document
 *
 * @ORM\Table(name="documents")
 */
class Document {
    public function ___clone(){
        $newObj = clone $this;
        $newObj->id = null;
        return $newObj;
    }
...
}

This does not seem to do much, however, as when I call clone myDocument and then try to persist, I still get this message:然而,这似乎没有多大作用,因为当我调用clone myDocument然后尝试坚持时,我仍然收到以下消息:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_A2B07288ECC6147F' SQLSTATE [23000]:完整性约束违规:1062 键“UNIQ_A2B07288ECC6147F”的重复条目“1”

How can I get my object's primary key to revert to a null or auto-incremented state?如何让对象的主键恢复为空或自动递增状态?

===== ======

Upate: Using更新:使用

public function __clone(){
    $this->id = null;
}

still results in the same error.仍然导致相同的错误。 Full error text:完整的错误文本:

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]:执行 'INSERT INTO 文档时发生异常?, ?, ?, ?, ?, ?, ?, ?, ?)' 带参数 ["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' SQLSTATE [23000]:完整性约束违规:1062 键“UNIQ_A2B07288ECC6147F”的重复条目“1”

This is not how PHP's cloning works.这不是PHP 克隆的工作方式。 Think of __clone like of __construct .__clone想成__construct In the __clone method, you must assign the new values to $this .__clone方法中,您必须将新值分配给$this

class Document 
{
    public function ___clone()
    {
        // simple as that
        $this->id = null;
    }
}

In your current code, the $newObj is going to be thrown away, while the cloned object still has the original ID.在您当前的代码中, $newObj将被丢弃,而克隆的对象仍然具有原始 ID。

Also, remember to clone child objects in your __clone method if you want to create a deep copy, otherwise you'll end up with two entities referencing the same children.另外,如果你想创建一个深拷贝,记得在你的__clone方法中克隆子对象,否则你最终会得到两个引用相同子对象的实体。 (Or, after persisting/reloading: One of the entities will have lost its children.) (或者,在持久化/重新加载之后:其中一个实体将失去它的孩子。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM