首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Symfony2:编辑可翻译实体的1个表单

Symfony2:编辑可翻译实体的1个表单
EN

Stack Overflow用户
提问于 2011-11-16 23:43:03
回答 3查看 4.1K关注 0票数 12

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

我正在尝试构建一个如下所示的表单:

代码语言:javascript
代码运行次数:0
运行
复制
   | French |English| Spanish |
+--+--------|       |---------+------------+
|                                          |
| name:  [___my_english_name___]           |
|                                          |
| title: [___my_english_title__]           |
|                                          |
+------------------------------------------+

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

因此,基本上,对象的order和online属性是不可翻译的,name & title属性具有可翻译的行为。

如果我的绘图不清楚:表单在每个区域设置中包含1个制表符,其中包含可翻译字段。

我遇到的问题是,默认情况下,Symfony2将一个表单绑定到一个实体,但是可翻译行为原则迫使我在每个地区都有一个实体。就个人而言,理论行为很好(我喜欢它),但我无法制作一个允许我在所有地区编辑实体的表单--以相同的形式。

到目前为止,我已经完成了主要的表单:

代码语言:javascript
代码运行次数:0
运行
复制
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';
    }
}

和制表符表单:

代码语言:javascript
代码运行次数:0
运行
复制
<?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';
    }
}

但正如您所看到的,我不知道如何从转换后的实体获取name和title值,也不知道如何在表单提交后将它们持久化。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-20 00:57:38

如果您使用gedmo extensions,那么Translatable并不意味着要为每个请求处理多个翻译。尝试使用knplabs alternative可能是以更通用的方式处理它的更好选择。

票数 6
EN

Stack Overflow用户

发布于 2012-11-29 16:58:25

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

票数 4
EN

Stack Overflow用户

发布于 2011-12-14 05:01:58

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

因此,我们开发了一个解决方案,并构建了一个我们决定共享的BreadGeneratorBundle:https://github.com/idealtech/BreadGeneratorBundle

在发表这篇文章的时候,它还在开发中,但它可以用作CrudGenerator的替代品,以便为可翻译的实体生成表单。

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

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8154379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档