【问题标题】:Sonata Admin - Unable to get sonata_type_collection working with modalSonata Admin - 无法让 sonata_type_collection 与模态一起使用
【发布时间】:2016-12-02 21:58:15
【问题描述】:

在我的管理员中,我定义了一个 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;

在我的管理类中,我将“模块”字段定义为:

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

最后在 ModuleSequence Entity 中是 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;
    }

我有“添加”按钮,我得到了模式,我填充并验证。 Ajax 请求被发送到分析器,但没有出现新行。

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

当我使用 'inline' 和 'table' 选项时,id 设置得很好。

【问题讨论】:

    标签: php symfony sonata-admin sonata


    【解决方案1】:

    遇到了同样的问题并通过覆盖 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); 
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      问题部分解决了...

      【讨论】:

        猜你喜欢
        • 2012-07-15
        • 2015-05-30
        • 2017-02-20
        • 2015-01-09
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        相关资源
        最近更新 更多