我已经按照示例 here 实现了翻译.
在我的实体中,我应该添加魔术方法__call
:
class Occupation
{
use ORMBehaviors\Translatable\Translatable;
/* ... attributes ... */
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
}
但是,当获取以下形式的数据时,不会调用此方法:
class PostJobStep1Type extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('occupation', EntityType::class, [
'label' => 'form.occupation',
'class' => Occupation::class,
'choice_label' => 'name'
]);
}
}
所以我收到一个错误:
Neither the property
name
nor one of the methodsgetName()
,name()
,isName()
,hasName()
,__get()
exist and have public access in classAppBundle\Entity\Occupation
.
有什么方法可以强制 Symfony
也检查魔术方法 __call
吗?
非常感谢
最佳答案
您的问题似乎与 PropertyAccessor 组件的默认配置有关。如前所述in the documentation ,默认情况下禁用允许使用__call
的功能:
The __call() feature is disabled by default, you can enable it by calling
PropertyAccessorBuilder::enableMagicCall
see Enable other Features.
由于有问题的属性访问器很可能是由您的表单自动构造的,因此您实际上无法调用 enableMagicCall
并且据我所知,无法更改此设置只有一种表单类型。
话虽这么说,您可以通过将以下条目添加到您的 services.yml
(取自 this discussion )来全局启用此功能,以便 magicCall
argument of the constructor可以为应用程序的所有 PropertyAccessor 设置为 true。
property_accessor:
class: Symfony\Component\PropertyAccess\PropertyAccessor
arguments: [true]
注意:在 SF2.8 中,您可以将完全限定的类名替换为 %property_accessor.class%
关于symfony - 使用表单时不会调用魔术方法 __call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747641/