Sonata Admin - 无法 sonata_type_collection 使用模态

Sonata Admin - Unable to get sonata_type_collection working with modal

在我的管理员中,我定义了一个 OneToMany:

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

反面:

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

在我的管理员 class 中,我将 'modules' 字段定义为:

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

最后在 ModuleSequence 实体中,这里是 addModule 方法:

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

        return $this;
    }

我有 "add" 按钮,我得到了模式,我填充它并验证。 Ajax 请求被发送到探查器但没有新行出现。

'sequence_id' 没有在数据库中设置,我不知道为什么...请问有什么想法吗?

当我使用 'inline' 和 'table' 选项时,id 设置正确。

有同样的问题并通过覆盖 prePersist 和 preUpdate 方法解决了它,然后持久化关联。

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

在此处对 Sonata Admin GitHub 进行长时间讨论后:

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

问题部分解决...