2

我已经按照此处的示例实施了翻译。

在我的实体中,我应该添加魔术方法__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'
        ]);
    }
}

所以我得到一个错误:

该属性name和方法之一getName(), name(), isName(),都不存在并且在类hasName()__get()具有公共访问权限AppBundle\Entity\Occupation

有什么办法也可以强制Symfony检查魔术方法__call吗?

非常感谢

4

1 回答 1

4

您的问题似乎与 PropertyAccessor 组件的默认配置有关。如文档__call中所述,默认情况下禁用允许使用的功能:

__call() 功能默认是禁用的,你可以通过调用see Enable other Features来启用它。PropertyAccessorBuilder::enableMagicCall

由于有问题的属性访问器很可能由您的表单自动构建,因此您实际上无法调用enableMagicCall,据我所知,无法仅针对一种表单类型更改此设置。

话虽如此,您可以通过将以下条目添加到您的services.yml(取自此讨论)中来全局启用此功能,以便可以为您的应用程序的所有 PropertyAccessormagicCall将构造函数的参数设置为 true。

property_accessor:
    class: Symfony\Component\PropertyAccess\PropertyAccessor
    arguments: [true]

注意:在 SF2.8 中,您可以将完全限定的类名替换为%property_accessor.class%

于 2018-02-12T16:24:17.940 回答