所以我有一个Symfony 4应用程序,它是我的主要实体
Structure
$referent
(基本上是联系信息)与以下关联一起存储:
/**
* @ORM\OneToMany(targetEntity="App\Entity\Referent", mappedBy="structure", cascade={"persist"})
*/
private $referent;
通过它的getter和setter:
public function getReferent(): Collection
{
return $this->referent;
}
public function addReferent(Referent $referent): self
{
if (!$this->referent->contains($referent)) {
$this->referent[] = $referent;
$referent->setStructure($this);
}
return $this;
}
public function removeReferent(Referent $referent): self
{
if ($this->referent->contains($referent)) {
$this->referent->removeElement($referent);
if ($referent->getStructure() === $this) {
$referent->setStructure(null);
}
}
return $this;
}
请注意,所有代码都是由自动生成的
$ php bin/console make:entity
问题是每当我保存一个新的
结构
StructureType
嵌入多个
ReferentType
表格(根据
the documentation
)都是
结构
和
Referent
实体保存在数据库中,但
structure_id
在
referent
表设置为
NULL
以下是处理表单显示和提交的控制器操作:
public function add(Request $request)
{
$structure = new Structure();
$form = $this->createForm(StructureType::class, $structure, [
'attr' => ['id' => 'add-form'],
]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$structure = $form->getData();
$manager = $this->getDoctrine()->getManager();
$manager->persist($structure);
$manager->flush();
return $this->redirectToRoute('back_update', ['id' => $structure->getId()]);
}
return $this->render('back/add.html.twig', [
'form' => $form->createView(),
'action' => __FUNCTION__,
]);
}
那为什么呢?
, cascade={"persist"}
在关系注释上,我在提交表单时出现以下错误:
任何帮助都将不胜感激。
编辑#1
根据要求,这里是关系的反面:
private $structure;
通过它的getter和setter:
public function getStructure(): ?Structure
{
return $this->structure;
}
public function setStructure(?Structure $structure): self
{
$this->structure = $structure;
return $this;
}