Question

I'm trying to manage a oneToMany relation with sonata admin

Actually I'd like to have manyToMany relation like sonata media bundle has with gallery > media

A HomePage has many Storys which should be sortable etc.
Theres also HomePageStory to manage the relation, as suggested here:
https://github.com/sonata-project/SonataAdminBundle/issues/1231

HomePage.orm.yml

    oneToMany:
        HomePageStorys:
            targetEntity: HomePageStory
            mappedBy: HomePage
            cascade: ["persist", "merge", "remove"]

HomePageAdmin.php

       ->add(
          'HomePageStorys',
          'sonata_type_collection',
          array(
              'cascade_validation' => true,
          ),
          array(
              'edit' => 'inline',
              'inline' => 'table',
              'sortable' => 'position'
          )
      );

HomePageStory.orm.yml

    manyToOne:
        HomePage:
            targetEntity: HomePage
            joinColumn:
                name: homepage_id
                referencedColumnName: id

        Story:
            targetEntity: Story
            joinColumn:
                name: story_id
                referencedColumnName: id

HomePageStoryAdmin

    protected function configureFormFields(FormMapper $formMapper)
    {
        $link_parameters = array();

        if ($this->hasParentFieldDescription()) {
            $link_parameters = $this->getParentFieldDescription()->getOption('link_parameters', array());
        }

        if ($this->hasRequest()) {
            $context = $this->getRequest()->get('context', null);

            if (null !== $context) {
                $link_parameters['context'] = $context;
            }
        }

        $formMapper
          ->add(
              'Story',
              'sonata_type_model_list',
              array('required' => false),
              array(
                  'link_parameters' => $link_parameters
              )
          )
          ->add('position', 'hidden');
    }

This works fine so far, as i can add HomePageStories on the HomePage Admin and pick a Story from the list inline.

But it just stores the story_id in the database while homepage_id stays empty. If I set the homepage_id by hand, it's displayed in the homepage admin.

Any clue what I need to do to store the id of the parent entity (HomePage) on save?

Était-ce utile?

La solution

hm, seems as if you manually have to set the parent entity on the related children...

While not sure if it is really necessary or "the official way", the following did the trick:

HomePageAdmin.php

public function prePersist($homepage)
{
    foreach ($homepage->getHomePageStorys() as $homepagestory) {
        $homepagestory->setHomePage($homepage);
    }
}

/**
 * {@inheritdoc}
 */
public function preUpdate($homepage)
{
    foreach ($homepage->getHomePageStorys() as $homepagestory) {
        $homepagestory->setHomePage($homepage);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top