代码之家  ›  专栏  ›  技术社区  ›  D4V1D

持久化具有OneToMany关系的父实体会使MySQL中的子实体\u id字段为空

  •  0
  • D4V1D  · 技术社区  · 6 年前

    所以我有一个Symfony 4应用程序,它是我的主要实体 Structure $referent (基本上是联系信息)与以下关联一起存储:

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Referent", mappedBy="structure", cascade={"persist"})
     */
    private $referent;
    

    通过它的getter和setter:

    /**
     * @return Collection|Referent[]
     */
    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);
            // set the owning side to null (unless already changed)
            if ($referent->getStructure() === $this) {
                $referent->setStructure(null);
            }
        }
    
        return $this;
    }
    

    请注意,所有代码都是由自动生成的 $ php bin/console make:entity

    问题是每当我保存一个新的 结构 StructureType 嵌入多个 ReferentType 表格(根据 the documentation )都是 结构 Referent 实体保存在数据库中,但 structure_id referent 表设置为 NULL

    enter image description here

    以下是处理表单显示和提交的控制器操作:

    /**
     * @Route("/add", name="back_add")
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    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"} 在关系注释上,我在提交表单时出现以下错误:

    enter image description here

    任何帮助都将不胜感激。


    编辑#1

    根据要求,这里是关系的反面:

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Structure", inversedBy="referent")
     */
    private $structure;
    

    通过它的getter和setter:

    public function getStructure(): ?Structure
    {
        return $this->structure;
    }
    
    public function setStructure(?Structure $structure): self
    {
        $this->structure = $structure;
    
        return $this;
    }
    
    2 回复  |  直到 4 年前
        1
  •  1
  •   letibelim    6 年前

    为了使Symfony表单正确地使用adder/remover处理集合,您需要添加选项“by_reference”=>在StructureType类中为false。是这样吗?

    $builder->add('referents', CollectionType::class, array('by_reference' => false))
    

    https://symfony.com/doc/current/form/form_collections.html

        2
  •  0
  •   Joao Victor Souza    6 年前

    缺少JoinColumn()注释

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Structure", inversedBy="referent")
     * @ORM\JoinColumn()
     */
    private $structure;