Symfony2: 1 форма для редактирования переводимой сущности

У меня есть переводимый объект, использующий переводимое поведение 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';
    }
}

Но, как вы не видите, я ' Я не знаю, как получить значения имени и заголовка из переведенного объекта, и я также не знаю, как сохранить их после того, как форма будет отправлена.

12
задан FMaz008 13 December 2011 в 21:26
поделиться