简体   繁体   English

Sonata管理员-无法使sonata_type_collection与模式一起使用

[英]Sonata Admin - Unable to get sonata_type_collection working with modal

In my admin I have a OneToMany defined as it: 在我的管理员中,我定义了一个OneToMany:

/**
 * @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: 最后,在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;
    }

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. Ajax请求被发送到探查器,但是没有新行出现。

The 'sequence_id' is not set in the database and I don't know why... Any idea please? 数据库中未设置'sequence_id',我也不知道为什么...请问有什么想法吗?

When I use the 'inline' & 'table' options, the id is well set. 当我使用'inline'和'table'选项时,ID设置正确。

Had the same issue and solved it with overriding the prePersist and preUpdate methods and then persist the associations. 遇到相同的问题,并通过覆盖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); 
    }
}

After a long discussion on Sonata Admin GitHub here: 经过在Sonata Admin GitHub上的长时间讨论后,在这里:

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

Problem partially solved... 问题已部分解决...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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