I have a problem with the SonataAdminBunle in combination with symfony 2.2. I have a Project entity and a ProjectImage entity and specified a One-to-Many relationship betwee
Since some things have changed with Symfony form collection now adding the addChild() and removeChild() with the by_reference option set to false automatically persist the Collection and set the ID on the inverse side as expected.
Here is a full working version: https://gist.github.com/webdevilopers/1a01eb8c7a8290d0b951
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('childs', 'sonata_type_collection', array(
'by_reference' => false
), array(
'edit' => 'inline',
'inline' => 'table'
))
;
}
The addChild() method must contain the setter for the parent on the child:
public function addChild($child)
{
$child->setParent($this); // !important
$this->childs[] = $child;
return $this;
}