Doctrine / Symfony2 OneToMany foreign_id saves as NULL

I have a relationship from Assembly to ComponentSlot. This is a OneToMany relationship.

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

The schema generated in the database is absolutely beautiful. The right columns, the right indexes and relationships.

The Symfony2 AssemblyType form has a ComponentSlotType collection. I can add several child components of ComponentSlot. When saved, all Assembly and ComponentSlot children are preserved perfectly, except that assembly_id is NULL in the component slot table.

I copied the settings that I had in the previous project, which perfectly preserved the relationship, I'm completely at a dead end. Cascading emphasis is set in the ComponSlots Assembly fields, and my past experience with OneToMany is that I don't need to do anything here, it should be taken care of.

Any pointers would be appreciated :)

+6
source share
1 answer

Check the previous setting. I suspect you have something like:

 // Assembly public function addComponentSlot($componentSlot) { $this->componentSlots[] = $componentSlot; $componentSlot->setAssembly($this); // Probably left this out when you copied? } 
+4
source

Source: https://habr.com/ru/post/907875/


All Articles