我正在尝试在我的包中实现一些可重用的翻译。这些是我的代码的相关部分:
TranslatorKeys.php:
...
/**
* @var \ArrayCollection
*
* @ORM\OneToMany(targetEntity="Jumi\TranslatorBundle\Entity\TranslatorStrings", mappedBy="key")
*/
private $strings;
/**
* @param Doctrine\Common\Collections\ArrayCollection $strings
*/
public function setStrings(ArrayCollection $strings) { // THIS IS LINE 84
$this->strings = $strings;
}
...
TranslatorStrings.php:
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="Jumi\TranslatorBundle\Entity\TranslatorLanguages", fetch="LAZY")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="language_id", referencedColumnName="id", nullable=false)
* })
*/
private $language;
/**
* @var string
*
* @ORM\Column(name="_value", type="string", nullable=true)
*/
private $value;
/**
* @param string $value
*/
public function setValue($value) {
$this->value = $value;
}
EmbeddableTranslatorKeyType.php:
注意:translator_strings 类型在 TranslatorStringsType.php 中定义
...
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('strings', 'collection', array(
'type' => 'translator_strings',
'options' => array(
'form_type' => $options['form_type'],
'required' => $options['required']
),
'prototype' => true,
'allow_add' => true,
'allow_delete' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Jumi\TranslatorBundle\Entity\TranslatorKeys',
'form_type' => 'text'
));
$resolver->addAllowedValues(array(
'form_type' => array(
'text',
'textarea'
)
));
}
...
TranslatorStringsType.php
...
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('value', $options['form_type'], array(
'label' => false,
'required' => $options['required']
));
$builder->add('language', 'hidden');
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'form_type' => 'text',
'data_class' => 'Jumi\TranslatorBundle\Entity\TranslatorStrings'
));
$resolver->addAllowedValues(array(
'form_type' => array(
'text',
'textarea'
)
));
}
...
我正在尝试以这种方式使用 embeddable_translator_key :
注意:标题是 TranslatorKeys 上的多对一关系
$builder->add('title', 'embeddable_translator_key', array(
'label' => 'label_title',
'required' => false
));
现在的问题是,我在将请求绑定到表单时遇到了这个异常:
Catchable Fatal Error: Argument 1 passed to
Jumi\TranslatorBundle\Entity\TranslatorKeys::setStrings() must be an instance of
Doctrine\Common\Collections\ArrayCollection, array given, called in
E:\prog\SymfonyExperiment\trunk\Symfony\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 350 and defined in
E:\prog\SymfonyExperiment\trunk\Symfony\src\Jumi\TranslatorBundle\Entity\TranslatorKeys.php line 84
如果我只是在 TranslatorKeys 实体的第 84 行删除 ArrayCollection 的参数类型定义,如果它是一个数组,则将 $strings 转换为 ArrayCollection,这是一个简单的解决方法,但我宁愿寻找合适的解决方案。
如果我没有提供足够的信息,请随时要求更多!
欢迎 Symfony 专家!