Symfony2:1形式编辑可翻译实体

时间:2011-11-16 15:43:03

标签: forms symfony doctrine-orm translation

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

我正在尝试构建一个看起来像这样的表单:

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

Order:  [___1___]
Online: (x) Yes
        ( ) No

所以基本上,有顺序&不可翻译的对象的在线属性,以及名称&具有可翻译行为的title属性。

如果我的绘图不清楚:表单包含每个区域设置的1个标签,用于保存可翻译的字段。

我遇到的问题是默认情况下,Symfony2将表单绑定到实体,但是可学习的可翻译行为迫使我每个语言环境都有一个实体。个人主义行为很好(我喜欢它),但我无法创建一个允许我在所有语言环境中编辑实体的表单 - 以相同的形式。

到目前为止,我的主要形式是:     

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

但是你看不清楚,我不知道如何从翻译过的实体中获取名称和标题值,而且一旦提交表单,我都不知道如何保留它们。

3 个答案:

答案 0 :(得分:6)

如果你使用gedmo extensions嗨,可翻译并不意味着每个请求处理多个翻译。尝试使用knplabs alternative可能是更好的选择,以更一般的方式处理它。

答案 1 :(得分:4)

您可能对TranslationFormBundle感兴趣,它添加了一种表单类型以使用DoctrineTranslatable扩展。

答案 2 :(得分:-1)

我检查了译者扩展,即使它很有趣,也不符合我们的需求。 (基本上,我们发现的所有示例都要求我们更改站点区域设置以便在另一个区域设置中编辑实体。我不懂中文,我不希望我的界面是中文,但我我有一个翻译,我必须复制/粘贴。似乎奇怪的解释,因为它是你在那里找到的每个固体CMS中的基本,但我看起来有点复杂使用Symfony做那种CMS功能。)

所以我们开发了一个解决方案并构建了一个我们决定分享的BreadGeneratorBundle: https://github.com/idealtech/BreadGeneratorBundle

在发布时,它仍在开发中,但它可以用作CrudGenerator的替代品,以便为可翻译实体生成表单。

我们还设法使用Gedmo扩展 - 即使Gediminas说它不打算处理多个翻译;)

希望这会对某人有所帮助! :)