Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine/Symfony2 OneToMany foreign_id saving as NULL

I have a relationship from Assembly to ComponentSlot. It 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 this has generated in the database is absolutely fine. Correct columns, correct indices and relations.

The Symfony2 form AssemblyType has a collection of ComponentSlotType. I am able to add multiple ComponentSlot children. On persisting, the Assembly and ComponentSlot children are all saved perfectly well, except that assembly_id is NULL in the component slot table.

I have copied the setup I had on a previous project that saved the relationships just fine, I am completely stumped. The cascade persist is set on the componentSlots fields of Assembly and my past experience with OneToMany is that I do not have to do anything special here, it should be taken care of.

Any pointers would be appreciated :)

like image 465
PorridgeBear Avatar asked Feb 07 '12 20:02

PorridgeBear


1 Answers

Check your previous setup. I suspect you had something like:

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

    $componentSlot->setAssembly($this);  // Probably left this out when you copied?
}
like image 186
Cerad Avatar answered Sep 26 '22 08:09

Cerad