【发布时间】: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 还比较陌生。请提供任何帮助。
【问题讨论】: