首页 > 解决方案 > 更新多对一关系

问题描述

我有一个实体,我想更新它的 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。

标签: phpsymfonydoctrine

解决方案


推荐阅读