如何在Form实体字段中使用已翻译的属性?

时间:2016-04-28 08:21:24

标签: symfony translation

在Symfony2中使用可翻译实体,我想在表单实体类型中使用已翻译的属性。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('connectors', 'collection', [
                'type' => 'entity',
                'options' => [
                    'class' => 'Rpc\Bundle\ChargingBundle\Entity\Type\ChargingConnectorBaseType',
                    'label' => 'rpc.backend.customer_website_country_data.form.connector',
                    'property' => 'title',
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('a')
                                    ->where('a.wire = :value')
                                    ->setParameter('value', false);
                    },
                ],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'error_bubbling' => false,
            ])

相应的实体:

namespace Rpc\Bundle\ChargingBundle\Entity\Type;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation as Serializer; // @see annotation Serializer
use Symfony\Component\Validator\Constraints as Assert; // @see annotations Assert
use Rpc\Bundle\ChargingBundle\Model\ImageInterface;
use Rpc\Bundle\ChargingBundle\Model\ChargingConnectorPictoImageInterface;

/**
 * Base Type of Charging Connector.
 *
 * @ORM\Table(name="charging_connector_base_type")
 * @ORM\Entity(repositoryClass="Rpc\Bundle\CoreBundle\Doctrine\ORM\EntityRepository")
 * @Gedmo\TranslationEntity(class="Rpc\Bundle\ChargingBundle\Entity\Translation\ChargingConnectorBaseTypeTranslation")
 * @Serializer\ExclusionPolicy("all")
 */
class ChargingConnectorBaseType extends Type
{

/**
 * @ORM\OneToMany(
 *   targetEntity="Rpc\Bundle\ChargingBundle\Entity\Translation\ChargingConnectorBaseTypeTranslation",
 *   mappedBy="object",
 *   cascade={"persist", "remove"},
 *   orphanRemoval=true
 * )
 */
protected $translations;

/**
 * @var string
 *
 * @Gedmo\Translatable
 * @ORM\Column(name="title", type="string", length=255)
 * @Assert\Length(max="255", groups={"type", "import_csv"})
 * @Assert\NotBlank(groups={"type", "import_csv"}, message="rpc.constraint.not_blank.type_title")
 */
protected $title;

我的问题是:"标题"属性未翻译就是这种情况。我总是得到默认语言环境的值。

如何在表单实体中正确翻译此属性?

0 个答案:

没有答案