Symfony2:一个表单编辑可翻译实体

12

我有一个可翻译的实体,使用doctrine2的可翻译行为。

我正在尝试构建一个类似于以下样式的表单:

   | French |English| Spanish |
+--+--------|       |---------+------------+
|                                          |
| name:  [___my_english_name___]           |
|                                          |
| title: [___my_english_title__]           |
|                                          |
+------------------------------------------+

Order:  [___1___]
Online: (x) Yes
        ( ) No
基本上,对象有订单和在线属性是不可翻译的,而名称和标题属性具有可翻译的行为。如果我的描述不够清楚:表单包含每个区域设置一个选项卡,其中包含可翻译的字段。我的问题是,默认情况下,Symfony2将表单绑定到实体,但是doctrine的可翻译行为要求我每个区域设置使用一个实体。个人而言,这种行为很好(我喜欢它),但我无法制作一个允许我在同一个表单中编辑所有区域设置实体的表单。到目前为止,我拥有主表单:
namespace myApp\ProductBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

/**
 * Form for the productGroup.
 */
class ProductType extends AbstractType
{
    /**
     * Decide what field will be present in the form.
     *
     * @param FormBuilder $builder FormBuilder instance.
     * @param array       $options Custom options.
     *
     * @return null;
     */
    public function buildForm(FormBuilder $builder, array $options)
    {
        //Todo: get the available locale from the service.
        $arrAvailableLocale = array('en_US', 'en_CA', 'fr_CA', 'es_ES');

        //Render a tab for each locale
        foreach ($arrAvailableLocale as $locale) {
            $builder->add(
                'localeTab_' . $locale,
                new ProductLocaleType(),
                array('property_path' => false, //Do not map the type to an attribute.
                     ));
        }


        //Uni-locale attributes of the entity.
        $builder
            ->add('isOnline')
            ->add('sortOrder');


    }

    /**
     * Define the defaults options for the form building process.
     *
     * @param array $options Custom options.
     *
     * @return array Options with the defaults values applied.
     */
    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'myApp\ProductBundle\Entity\Product',
        );
    }

    /**
     * Define the unique name of the form.
     *
     * @return string
     */
    public function getName()
    {
        return 'myapp_productbundle_producttype';
    }
}

还有选项卡表单:

<?php

namespace MyApp\ProductBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

use invalidArgumentException;

/**
 * Form for the productGroupLocale tabs.
 */
class ProductLocaleType extends AbstractType
{
    /**
     * Decide what field will be present in the form.
     *
     * @param FormBuilder $builder FormBuilder instance.
     * @param array       $options Custom options.
     *
     * @return null;
     */
    public function buildForm(FormBuilder $builder, array $options)
    {


        $builder->add('name', 'text', array('data' => ???));
        $builder->add('title', 'text', array('data' => ???));

    }

    /**
     * Define the defaults options for the form building process.
     *
     * @param array $options Custom options.
     *
     * @return array Options with the defaults values applied.
     */
    public function getDefaultOptions(array $options)
    {
        return array(
            //'data_class' => 'MyApp\ProductBundle\Entity\Product',
            'name' =>  '',
            'title' => '',
        );
    }

    /**
     * Define the unique name of the form.
     *
     * @return string
     */
    public function getName()
    {
        return 'myapp_productbundle_productlocaletype';
    }
}

但是正如你看不到的那样,我不知道如何从翻译后的实体中获取名称和标题值,也不知道如何在表单提交后将它们持久化。


不知道是否能帮到其他人,但我认为我已经理解了类型仅构建表单的“结构”。数据是在其他地方“注入”的,但我仍然无法弄清楚在哪里。 - FMaz008
3个回答

6

您好,如果您使用gedmo扩展,Translatable并不适用于处理每个请求的多个翻译。尝试使用knplabs替代方案可能是更好的选择,以更普遍的方式处理它。


最后一个链接已经失效了。有没有翻译器扩展的文档?谢谢。 - A.L

4
您可能会对TranslationFormBundle感兴趣,它添加了一个表单类型,可与DoctrineTranslatable扩展一起使用。

-1
我已经检查了翻译扩展,即使它很有趣,但它并不符合我们的需求。(基本上,我们找到的所有示例都要求我们更改站点语言环境,以便在另一个语言环境中编辑实体。我不懂中文,也不想让我的界面变成中文,但我确实有一个需要复制/粘贴的翻译。似乎很奇怪要解释这一点,因为在任何可靠的CMS中,这都是非常基本的功能,但我正在寻找使用Symfony实现这种CMS功能的比较复杂的方法。)
因此,我们开发了一个解决方案,并构建了一个BreadGeneratorBundle,我们决定分享: https://github.com/idealtech/BreadGeneratorBundle 发布时,它仍在开发中,但可以用作CrudGenerator的替代品,以生成可翻译实体的表单。
我们还成功地使用了Gedmo扩展,尽管Gediminas说它不适用于处理多个翻译 ;)
希望这能帮助到某些人! :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接