【问题标题】:Overload the __clone method of an object to set its ID to null重载对象的 __clone 方法以将其 ID 设置为 null
【发布时间】:2016-11-02 15:38:34
【问题描述】:

我想克隆对象并在数据库中保存一个副本,但问题是需要将ID重置为null才能获取数据库中的自动序列号。

我正在使用 Doctrine 和 Symfony2。我知道我需要修改我的实体的 __clone 方法,但我不知道如何在其中插入什么。

我有一个基本的克隆动作:

public function cloneAction()
{
    $object = $this->admin->getSubject();

    if (!$object) {
        throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
    }

    // Be careful, you may need to overload the __clone method of your object
    // to set its id to null !
    $clonedObject = clone $object;

    $this->admin->create($clonedObject);

    $this->addFlash('sonata_flash_success', 'Cloned successfully');

    return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
}

Sonata 的文档说我必须这样做,但我以前从未使用过 __clone,而且我对 PHP 还比较陌生。请提供任何帮助。

【问题讨论】:

标签: php symfony


【解决方案1】:

没有问题。没有必要重载 __clone 方法。我所要做的就是修复我的实体映射,它就成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    相关资源
    最近更新 更多