(Symfony-newbie here),我有一些基本代码可以更新DB中的字段。
public function editAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$testowanie = $em->getRepository('TestowanieBundle:Test')->findOneBy(array('id' => $testowanieId));
$form = $this->createForm(TestowanieType::class, $testowanie);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($testowanie);
$em->flush();
return $this->redirectToRoute('...');
}
此函数从DB中的行获取数据并覆盖它,但我想根据当前保存在DB中的数据插入具有新id的副本。
答案 0 :(得分:2)
How to re-save the entity as another row in Doctrine 2
如本文所述,您应该重置ID或只使用Clone关键字,如下所示:
if ($form->isSubmitted() && $form->isValid()) {
$new_entity = clone $testowanie;
$em->persist($new_entity);
$em->flush();
return $this->redirectToRoute('...');
}