PorridgeBear
PorridgeBear

Reputation: 1183

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 :)

Upvotes: 6

Views: 2133

Answers (1)

Cerad
Cerad

Reputation: 48865

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?
}

Upvotes: 4

Related Questions