php - 更新多对一关系
问题描述
我有一个实体,我想更新它的 ManyToOne 关系。我有 FicheSynthese ManyToOne Demandeur 关系。当我想编辑 FicheSynthese 对象时,我会发送 FicheSynthese 和 Demandeur 表单,其中填充了数据库中的信息。
用户可以决定将 Demandeur 信息更改为全新的信息。我使用 AJAX 调用处理这种情况,如果 Demandeur 不存在,我将 Demandeur 表单的 ID 字段设置为 -1。在这种特定情况下,我想创建一个 Demandeur 对象(并在数据库中)并更新 FicheSynthese<->Demandeur 关系。
小备注 Demandeur 是一个抽象类,在 4 个不同的类中继承。在版本中,用户可以更改 Demandeur 的类型或相同类型但仍是新的 Demandeur。
我已经读过好几次了,更新时我必须更新拥有该关系的实体。如果我理解正确,这里是 FicheSynthese。
我的实体的一些代码:
Demandeur.php
/**
*@var Collection|FicheSynthese[]
*
*@ORM\OneToMany(targetEntity="FicheSynthese", mappedBy="demandeur", fetch="EXTRA_LAZY")
*
*/
private $ficheSynthese;
public function __construct()
{
$this->ficheSynthese = new ArrayCollection();
}
/**
* @return Collection|FicheSynthese[]
*/
public function getFicheSynthese(): Collection
{
return $this->ficheSynthese;
}
public function addFicheSynthese(FicheSynthese $ficheSynthese): self
{
if (!$this->ficheSynthese->contains($ficheSynthese)) {
$this->ficheSynthese[] = $ficheSynthese;
$ficheSynthese->setDemandeur($this);
}
return $this;
}
public function removeFicheSynthese(FicheSynthese $ficheSynthese): self
{
if ($this->ficheSynthese->contains($ficheSynthese)) {
$this->ficheSynthese->removeElement($ficheSynthese);
// set the owning side to null (unless already changed)
if ($ficheSynthese->getDemandeur() === $this) {
$ficheSynthese->setDemandeur(null);
}
}
return $this;
}
FicheSynthese.php
/**
* @var \Demandeur
*
* @ORM\ManyToOne(targetEntity="Demandeur",
* inversedBy="ficheSynthese", cascade={"persist"})
* @ORM\JoinColumn(name="idDemandeur", referencedColumnName="id")
*/
private $demandeur;
public function getDemandeur(): ?Demandeur
{
return $this->demandeur;
}
public function setDemandeur(?Demandeur $demandeur): self
{
$this->demandeur = $demandeur;
return $this;
}
现在我的控制器(有点乱):
$em = $this->getDoctrine()->getManager('fichesynthese');
//My Demandeur form
$formsDem[$typeDem]->handleRequest($request);
//I get my Demandeur entity filled
$dem = $formsDem[$typeDem]->getData();
//I get the ID
$id = $dem->getId();
//If ID is -1 that means that my Demandeur doesn't exist
if ($id == -1) {
//I detach the old Demandeur entity, I don't want to remove it
$em->detach($dem);
//I create a new Demandeur entity (which should be the same as $dem)
$newDem = $formsDem[$typeDem]->getData();
//I attach the $newDem to my FicheSynthese that I'm currently editing
$fs->setDemandeur($newDem);
$em->flush();
}
我从 Symfony 开始,所以我对 Doctrine 和 Entities 等的理解是我不必坚持 $fs 因为它是一个版本形式。如果我转储 $fs,我会考虑 Demandeur 的变化。问题出在数据库中,它创建了新的 Demandeur,但没有更改 FicheSynthese 表中的外键。此外,它不会更新 Demandeur 实体中的关系。
我的猜测是,使用 -1 ID,它无法更新关系,但我不明白它如何在完全创建(FicheSynthese 和 Demandeur)时工作。
更多信息,在我的控制器中,我有一个用于 FicheSynthese 的表单和一个用于 Demandeur 的数组表单,其中包含所有类型的表单。通过继承,我没有找到如何将它嵌入到 FicheSynthese 形式中。
我在 Doctrine and update 中有什么不明白的地方?谢谢。
编辑:添加getter/setter。
解决方案
推荐阅读
- aws-lambda - 使用 pydub 和 AWS Lambda
- r - 仅在 Shiny 中插入 inputFile 时才显示 SliderInput
- python - 在计算熊猫数据框中的行均值时,iterrows() 中出现意外的 KeyError
- javascript - PHP 在 HTML 中显示实时 Shell 输出
- r - 仅计算正数日期之间的平均付款
- php - 如何从管理员登录页面登录,其他人应该从 laravel 5.8 中的另一个登录页面登录
- c - 为什么“C”代码有效而不是重新声明错误?
- java - Apache DefaultHttpClient - java.net.BindException:地址已在使用中:连接
- .net - MassTransit 更改交换类型问题
- sql - 连续几十年按十年排名前 N(在 SQL Server 中)