带有a2lix_translations和Gedmo教义扩展的Symfony2翻译表单

时间:2014-08-28 10:18:40

标签: forms symfony doctrine-orm translation

我在Symfony 2.5工作

  • gedmo / doctrine-extensions":" dev-master
  • a2lix / translation-form-b​​undle":" 2。* @ dev

我正在尝试使用值名称和说明在我的实体Collection中添加翻译。

这是我的实体集合:

    /**
 * Collection
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Angeli\AdminBundle\Entity\CollectionRepository")
 * @Gedmo\TranslationEntity(class="Angeli\AdminBundle\Entity\CollectionTranslation")
 */
class Collection
{
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @Gedmo\Translatable
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @var string
 *
 * @Gedmo\Translatable
 * @ORM\Column(name="description", type="text")
 */
private $description;

/**
 * @ORM\OneToMany(targetEntity="CollectionTranslation", mappedBy="object", cascade={"persist", "remove"})
 */
protected $translations;

/**
 * Required for Translatable behaviour
 * @Gedmo\Locale
 */
protected $locale;

public function __construct()
{
    $this->translations = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Collection
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set description
 *
 * @param string $description
 * @return Collection
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

/**
 * Get description
 *
 * @return string
 */
public function getDescription()
{
    return $this->description;
}

public function getTranslations()
{
    return $this->translations;
}

public function addTranslation(CollectionTranslation $t)
{
    $this->translations->add($t);
    $t->setObject($this);
}

public function removeTranslation(CollectionTranslation $t)
{
    $this->translations->removeElement($t);
}

public function setTranslations($translations)
{
    $this->translations = $translations;
}

public function __toString()
{
    return $this->getName();
}
}

这是我的CollectionTranslation类:

/**
 * @ORM\Entity
 * @ORM\Table(name="collection_translations",
 *     uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
 *         "locale", "object_id", "field"
 *     })}
 * )
 */
class CollectionTranslation extends AbstractPersonalTranslation
{
    /**
     * Convinient constructor
     *
     * @param string $locale
     * @param string $field
     * @param string $content
     */
    public function __construct($locale = null, $field = null, $content = null)
    {
        $this->setLocale($locale);
        $this->setField($field);
        $this->setContent($content);
    }

    /**
     * @ORM\ManyToOne(targetEntity="Collection", inversedBy="translations")
     * @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $object;
}

现在我正在尝试构建表单:

class CollectionType extends AbstractType{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    //$builder->add('name','text', array('required'  => true, 'label'=>'Name'));
    //$builder->add('description','textarea', array('required'  => true, 'label'=>'Description'));
    $builder->add('translations', 'a2lix_translations');


}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Angeli\AdminBundle\Entity\Collection',
    ));
}

public function getName()
{
    return 'Collection';
}

} 

但我的表格有3种语言标签 和输入字段

  • 字段
  • 内容

如果我添加

  • field = name
  • content = some text

我只翻译了值" name"

我想要一个带有我的语言标签的表单," name"和"描述"作为翻译表单中的输入字段。

有人看到我做错了吗?

2 个答案:

答案 0 :(得分:0)

试试这个:对于前。您使用的是英语,法语和德语3种语言。

在表单中添加:

$builder->add('translations', 'a2lix_translations', array(
        'locales' => array('en_US','fr_FR', 'de'),
        'required_locales'=>array('en_US'),
        'fields' => array(
            'name' => array(
                'field_type' => 'text',
                'label' => 'Name',                    
                'locale_options' => array(
                    'fr' => array(
                        'label' => 'nom'
                    ),
                    'de' => array(
                      'label' => 'Name'
                  ),
                )),
            'description' => array(
              'field_type' => 'textarea',
              'label' => 'Description',                    
              'locale_options' => array(
                  'fr' => array(
                      'label' => 'description'
                  ),
                  'de' => array(
                      'label' => 'Beschreibung'
                  ),
              )),
            ))); 

在视图中呈现

form_label(form.translations)

form_widget(form.translations)

答案 1 :(得分:0)

我和你的问题一样。 请尝试:

"gedmo/doctrine-extensions": "dev-master",
"a2lix/translation-form-bundle": "1.*@dev"

$builder->add('translations', 'a2lix_translations_gedmo', array(
    'translatable_class' => 'Angeli\AdminBundle\Entity\Collection',
));

CollectionTranslation 中也不需要 __ construct