Sonata Admin Bundle One-to-Many relationship not saving foreign ID

前端 未结 6 1331
粉色の甜心
粉色の甜心 2020-12-14 12:23

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

6条回答
  •  青春惊慌失措
    2020-12-14 13:07

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

提交回复
热议问题