gpt4 book ai didi

symfony - Doctrine/Symfony2 OneToManyforeign_id 保存为 NULL

转载 作者:行者123 更新时间:2023-12-02 18:36:45 26 4
gpt4 key购买 nike

我有从 Assembly 到 ComponentSlot 的关系。这是一个OneToMany 关系。

// Assembly

/**
* @ORM\OneToMany(targetEntity="ComponentSlot", mappedBy="assembly", cascade={"persist"})
* @Assert\Valid
*/
protected $componentSlots;

// ComponentSlot

/**
* @ORM\ManyToOne(targetEntity="Assembly", inversedBy="componentSlots")
*/
protected $assembly;

在数据库中生成的模式绝对没问题。正确的列、正确的索引和关系。

Symfony2 表单 AssemblyType 有一个 ComponentSlotType 的集合。我可以添加多个 ComponentSlot 子项。在持久化过程中,Assembly 和 ComponentSlot 子级都保存得很好,只是组件槽表中的 assembly_id 为 NULL。

我复制了之前项目中的设置,很好地保存了关系,我完全被难住了。级联持久化是在 Assembly 的 componentSlots 字段上设置的,我过去使用 OneToMany 的经验是,我不必在这里做任何特殊的事情,应该照顾好它。

任何指示将不胜感激:)

最佳答案

检查您之前的设置。我怀疑你有类似的事情:

// Assembly
public function addComponentSlot($componentSlot)
{
$this->componentSlots[] = $componentSlot;

$componentSlot->setAssembly($this); // Probably left this out when you copied?
}

关于symfony - Doctrine/Symfony2 OneToManyforeign_id 保存为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com