symfony - 使用表单时不会调用魔术方法 __call

标签 symfony symfony-3.4

我已经按照示例 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 methods getName(), name(), isName(), hasName(), __get() exist and have public access in class AppBundle\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/

相关文章:

netbeans - 在 Netbeans 7.1 下运行 Symfony 2 应用程序的 PHPUnit 测试

php - 交响乐 5 : How to make Login redirect to different pages deppending on role

symfony - Composer 更新 "man-in-the-middle attack"错误

php - 如何在 Symfony 3 中集成 Select2 JS

php - 创建自定义存储库原则

php - Symfony3.4 - POST_SUBMIT 事件的预填充字段

symfony - 如何在 Symfony 中的表单中添加未绑定(bind)的字段,否则绑定(bind)到实体?

php - 如何使用 Symfony 按其他实体对实体进行分组

unit-testing - Controller 单元测试 (Symfony)

docker - 如何在 docker 容器中运行 Symfony 控制台命令