我有一个使用doctrine2的可翻译行为的可翻译实体。
我正在尝试构建一个如下所示的表单:
| French |English| Spanish |
+--+--------| |---------+------------+
| |
| name: [___my_english_name___] |
| |
| title: [___my_english_title__] |
| |
+------------------------------------------+
Order: [___1___]
Online: (x) Yes
( ) No
因此,基本上,对象的order和online属性是不可翻译的,name & 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';
}
}
但正如您所看到的,我不知道如何从转换后的实体获取name和title值,也不知道如何在表单提交后将它们持久化。
发布于 2011-11-19 16:57:38
如果您使用gedmo extensions,那么Translatable并不意味着要为每个请求处理多个翻译。尝试使用knplabs alternative可能是以更通用的方式处理它的更好选择。
发布于 2012-11-29 08:58:25
您可能会对TranslationFormBundle感兴趣,它为DoctrineTranslatable扩展添加了一种表单类型。
发布于 2011-12-13 21:01:58
我已经检查了Translator扩展,即使它很有趣,它也不符合我们的需求。(基本上,我们找到的所有示例都要求我们更改站点区域设置,以便在另一个区域设置中编辑实体。我不懂中文,我也不希望我的界面是中文的,但我确实有一个翻译,我必须复制/粘贴。解释这一点似乎很奇怪,因为它在你会发现的每一个实体CMS中都是非常基本的,但我看起来使用Symfony来实现这种CMS功能有点复杂。)
因此,我们开发了一个解决方案,并构建了一个我们决定共享的BreadGeneratorBundle:https://github.com/idealtech/BreadGeneratorBundle
在发表这篇文章的时候,它还在开发中,但它可以用作CrudGenerator的替代品,以便为可翻译的实体生成表单。
我们还设法使用了Gedmo扩展--即使Gediminas说它不是用来处理多个翻译的;)
希望这能对某些人有所帮助!:)
https://stackoverflow.com/questions/8154379
复制