简体   繁体   中英

Sonata Admin - Unable to get sonata_type_collection working with modal

In my admin I have a OneToMany defined as it:

/**
 * @ORM\OneToMany(targetEntity="Module", mappedBy="sequence", cascade={"persist", "remove"})
 */
 private $modules;

And the inversed side:

/**
 * @ORM\ManyToOne(targetEntity="ModuleSequence", inversedBy="modules", cascade={"persist"}, fetch="LAZY")
 * @ORM\JoinColumn(name="sequence_id", referencedColumnName="id")
 */
protected $sequence;

In my admin class I defined the 'modules' field as it:

->add('modules', 'sonata_type_collection',array(
       'by_reference' => false
 ))

Finally in the ModuleSequence Entity here's the addModule method:

     /**
     * Add modules
     *
     * @param \AppBundle\Entity\Module $module
     * @return ModuleSequence
     */
    public function addModule(\AppBundle\Entity\Module $module)
    {
        $module->setSequence($this);
        $this->modules[] = $module;

        return $this;
    }

I have the "add" button, I get the modal, I fill it and validate. The Ajax request is sent into the profiler but no new row appear.

The 'sequence_id' is not set in the database and I don't know why... Any idea please?

When I use the 'inline' & 'table' options, the id is well set.

Had the same issue and solved it with overriding the prePersist and preUpdate methods and then persist the associations.

public function prePersist($object)
{
    $this->persistBlocks($object);

    $content->mergeNewTranslations();
}

public function preUpdate($object)
{
    $this->prePersist($object);
}

private function persistModules($object)
{
    $em = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager');

    foreach($object->getModules() as $module)
    {
        $module->setObject($object); // change Object to the name of your Entity!!
        $em->persist($module); 
    }
}

After a long discussion on Sonata Admin GitHub here:

https://github.com/sonata-project/SonataAdminBundle/issues/4236

Problem partially solved...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM