Entity's id of parent is not saved in a OneToMany relationship in SonataAdmin Entity's id of parent is not saved in a OneToMany relationship in SonataAdmin php php

Entity's id of parent is not saved in a OneToMany relationship in SonataAdmin


You need to manually attach the step to the tasks, to do so you need to use the prePersist and preUpdate methods in the step admin class...

The reason for this is that the developers of the SonataAdminBundle say it's doctrine's concern to handle this and the Doctrine developers say it is the bundles responsibility... So for now we need to do it for ourselves.

This would be your new stepAdmin class:

<?phpnamespace IMA\ProcessManagementBundle\Admin;use Sonata\AdminBundle\Admin\Admin;use Sonata\AdminBundle\Datagrid\ListMapper;use Sonata\AdminBundle\Datagrid\DatagridMapper;use Sonata\AdminBundle\Form\FormMapper;class StepAdmin extends Admin{    // Fields to be shown on create/edit forms    protected function configureFormFields(FormMapper $formMapper)    {        $formMapper            ->add('name', 'text', array('label' => 'Nom de l\'étape'))            ->add('tasks', 'sonata_type_collection', array(), array(                'edit' => 'inline',                'inline' => 'table',                'sortable'  => 'positionNumber'            ))            ->add('positionNumber', 'integer', array('label' => 'Position'))        ;    }    // Fields to be shown on filter forms    protected function configureDatagridFilters(DatagridMapper $datagridMapper)    {        $datagridMapper            ->add('name')        ;    }    // Fields to be shown on lists    protected function configureListFields(ListMapper $listMapper)    {        $listMapper            ->addIdentifier('name')            ->add('slug')        ;    }    public function prePersist($object)    {        foreach ($object->getTasks() as $task) {            $task->setStep($object);        }    }    public function preUpdate($object)    {        foreach ($object->getTasks() as $task) {            $task->setStep($object);        }    }}


In your Step entity you have to add in the addTask method :

class Step{    //...    public function addTask($tasks)    {        $tasks->setStep($this);        $this->tasks[] = $tasks;        return $this;    }    //...}

As you haven't give your Step.php you should propably adapt this code.