是否可以在 Sonata Admin Bundle 中添加可翻译的关联?

Posted

技术标签:

【中文标题】是否可以在 Sonata Admin Bundle 中添加可翻译的关联?【英文标题】:Is it possible to add a translatable association in Sonata Admin Bundle? 【发布时间】:2015-10-09 07:58:05 【问题描述】:

是否可以使用 DoctrineBehaviors 可翻译功能在 Sonata Admin 中添加可翻译关联?

我的意思是,类似的东西:

// InfoPageAdmin.php

->add('translations', 'a2lix_translations', [
    'fields' => [
        'title' => [
            'field_type' => 'text'
        ],
        'content' => [
            'field_type' => 'ckeditor',
            'config_name' => 'default'
        ],
        'slideshow' => [
            'field_type' => 'sonata_type_model_list'
        ]
    ]
])

“幻灯片”是可翻译字段,与其他实体相关联:

// InfoPageTranslation.php

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\PictureCollection", cascade="persist", fetch="EAGER")
 * @ORM\JoinColumn(name="slideshow_id", referencedColumnName="id")
 */
protected $slideshow;

我收到以下错误:

ContextErrorException:可捕获的致命错误:参数 1 传递给 Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer::__construct() 必须实现接口 Sonata\AdminBundle\Model\ModelManagerInterface,给定 null,调用 D:\XAMPP\htdocs\mega\app\cache\dev\classes.php 在第 13492 行和 在 D:\XAMPP\htdocs\mega\app\cache\dev\classes.php 第 12628 行中定义

我希望我的问题很清楚。

谢谢!

【问题讨论】:

【参考方案1】:

嗯,我找到了解决问题的简单方法。例如,我想为 InfoPage 的每种不同语言创建一个不同的图库。所以,我可以通过这种方式实现:

# InfoPageAdmin.php
->add('translations', 'a2lix_translations', [
    'fields' => [
        'gallery' => [
            'field_type' => 'entity',
            'class' => 'AppBundle:Gallery',
        ],
    ],
])

这里,Gallery 是 InfoPage 实体的字段:

# AppBundle/Entity/InfoPage.php
/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Gallery", cascade="persist", fetch="EAGER")
 * @ORM\JoinColumn(name="gallery_id", referencedColumnName="id")
 */
protected $gallery;

我希望我的回答对某人有所帮助。 :)

编辑:如果您想在翻译中使用“sonata_type_model_list”,此处描述了工作解决方法:https://github.com/a2lix/TranslationFormBundle/issues/155。

【讨论】:

以上是关于是否可以在 Sonata Admin Bundle 中添加可翻译的关联?的主要内容,如果未能解决你的问题,请参考以下文章

Sonata Admin Bundle - 表单类型:sonata_type_collection - 自定义模板?

Symfony 4 + Sonata + Sonata Doctrine ORM Admin Bundle:错误:没有要处理的元数据类

Sonata Admin Bundle 删除 routeCollection 角色

Sonata Admin Bundle:在列表视图中显示收藏总数

sonata-project/user-bundle dev-master 与 sonata-project/doctrine-orm-admin-bundle 冲突

Sonata admin-bundle - 没有注销导航栏