错误消息:
通过这种关系找到了一个新的实体 '的appbundle \实体\塔里#比索'没有配置为级联 坚持实体运营: 的appbundle \实体\ TarifaPeso @ 0000000072d3bd4300000000232470d3。要解决 此问题:在此显式调用EntityManager#persist() 未知实体或配置级联持久化此关联 映射例如@ManyToOne(..,cascade = {" persist"})。如果你不能 找出导致问题实施的实体 '的appbundle \实体\ TarifaPeso #__的toString()'得到一个线索。
Tarifa.php
/**
* @ORM\OneToMany(targetEntity="TarifaPeso", mappedBy="tarifa")
*/
private $pesos;
TarifaPeso.php
/**
* @ORM\ManyToOne(targetEntity="Tarifa", inversedBy="pesos", cascade={"persist"})
* @ORM\JoinColumn(name="tarifa_id", referencedColumnName="id")
*/
private $tarifa;
TarifaType.php
->add('pesos', CollectionType::class, array(
'entry_type' => TarifaPesoType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
控制器......
public function newAction(Request $request)
{
$tarifa = new Tarifa();
$form = $this->createForm('AppBundle\Form\TarifaType', $tarifa);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($tarifa);
$entityManager->flush();
$this->addFlash('success', 'project.created_successfully');
return $this->redirectToRoute('admin_post_index');
}
return $this->render('admin/tarifas/new.html.twig', array(
'form' => $form->createView(),
));
}
我错过了什么?真的很累...请帮忙吗?
答案 0 :(得分:3)
您应该将cascade={"persist"}
注释从TarifaPeso::tarifa
移至Tarifa::pesos
属性。或者您可以明确地保留从表单中获得的所有pesos
:
$entityManager->persist($tarifa);
foreach ($tarifa->getPesos() as $peso) {
$entityManager->persist($peso);
}
$entityManager->flush();
答案 1 :(得分:0)
好的,它已经解决了,现在它将Tarifa id存储在TarifaPeso表中。错误是我从AddPeso方法中删除了该行:
public function addPeso(\AppBundle\Entity\TarifaPeso $pesos)
{
$this->pesos[] = $pesos;
$pesos->setTarifa($this);
return $this;
}