我克隆一个实体并且在我持久化之后调用prePersist函数来设置创建日期和用户,并且当返回时,flush函数为我克隆的实体提供一个Undefined索引
克隆功能是:
public function cloneService($id)
{
$manager = $this->container->get('doctrine')->getManager();
$repository = $this->em->getRepository(Article::class);
$article = $repository->find($id);
$clonedObject = clone $article;
$clonedObject->setTitle('[Duplicate]'.$article->getTitle());
$clonedObject->setEnabled(false);
$manager->persist($clonedObject);
$manager->flush();
return ;
}
和prePersist是
public function prePersist(LifecycleEventArgs $args)
{
if ($this->tokenStorage->getToken() === null ) {
return ;
}
$user = $this->tokenStorage->getToken()->getUser();
$object = $args->getEntity();
if ($this->hasDateTimeTrait($object)) {
$this->setCreated($user, $object);
$this->setUpdated($user, $object);
}
}
,结果是
DoctrineORMEntityManager_0000000041a2ac07000000000bca81cc14d7185c1fc2068ccb74c3ea035ec2eb->齐平() 在src / Exozet / AcademyBundle / Service / CloneService.php(第45行) $ clonedObject->的setEnabled(假); $管理器 - >坚持($ clonedObject); $ manager = $这个 - >容器 - >获得( '教义') - > getManager(); $ repository = $这个 - > EM-> getRepository(文章::类); $管理器 - >冲洗();
答案 0 :(得分:0)
尝试
...
$clonedObject = clone $article;
$clonedObject->setId(null);
...