如何在表单类型中使用 KNP Translatable
How to use KNP Translatable in a form type
我正在使用 KNP Translatable,我有以下数据结构:
用户(id、姓名、电子邮件、密码...)
角色(id,名字@translatable)
用户角色是多对多关系。
我将表单类型定义为:
->add('roles', 'entity', [
'class' => 'SocialCarBackendBundle:Role',
'property' => 'name',
'multiple' => true,
'expanded' => true
])
并且我在角色实体中实现了__call方法:
public function __call($method, $arguments)
{
try {
return $this->proxyCurrentLocaleTranslation($method, $arguments);
} catch (\Symfony\Component\Debug\Exception\ContextErrorException $e) {
return $this->proxyCurrentLocaleTranslation('get' . ucfirst($method), $arguments);
}
}
现在,在 twig 模板中,我可以毫无问题地调用角色的名称 属性,并且它会正确呈现它。
但是在尝试呈现表单时出现此错误:
Neither the property "name" nor one of the methods "getName()",
"name()", "isName()", "hasName()", "__get()" exist and have public
access in class "SocialCar\BackendBundle\Entity\Role".
有什么解决方法吗?非常感谢
symfony 的 属性accessor 组件没有为 EntityType 属性
启用魔法调用
你可以看vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php来证明。
所以你有三种方法(按复杂程度排序):
做 getter 和调用 proxyCurrentLocaleTranslation 的设置器,恕我直言,使用较少的魔法东西没有什么不好:)
使用更复杂的 属性 像这样
'property' => 'translations[' 。 $选项['locale']。 '].name',
其中 $options['locale'] 是作为选项注入到表单中的语言环境
您可以创建一个不同的 EntityType class 来扩展您的自定义 DoctrineType class 来初始化 PropertyAccessor 以支持魔法调用
有关 属性 访问器的更多信息:
http://symfony.com/doc/current/components/property_access/introduction.html
关于第二种方式:
我正在使用 KNP Translatable,我有以下数据结构:
用户(id、姓名、电子邮件、密码...) 角色(id,名字@translatable)
用户角色是多对多关系。
我将表单类型定义为:
->add('roles', 'entity', [
'class' => 'SocialCarBackendBundle:Role',
'property' => 'name',
'multiple' => true,
'expanded' => true
])
并且我在角色实体中实现了__call方法:
public function __call($method, $arguments)
{
try {
return $this->proxyCurrentLocaleTranslation($method, $arguments);
} catch (\Symfony\Component\Debug\Exception\ContextErrorException $e) {
return $this->proxyCurrentLocaleTranslation('get' . ucfirst($method), $arguments);
}
}
现在,在 twig 模板中,我可以毫无问题地调用角色的名称 属性,并且它会正确呈现它。
但是在尝试呈现表单时出现此错误:
Neither the property "name" nor one of the methods "getName()", "name()", "isName()", "hasName()", "__get()" exist and have public access in class "SocialCar\BackendBundle\Entity\Role".
有什么解决方法吗?非常感谢
symfony 的 属性accessor 组件没有为 EntityType 属性
启用魔法调用你可以看vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php来证明。
所以你有三种方法(按复杂程度排序):
做 getter 和调用 proxyCurrentLocaleTranslation 的设置器,恕我直言,使用较少的魔法东西没有什么不好:)
使用更复杂的 属性 像这样
'property' => 'translations[' 。 $选项['locale']。 '].name',
其中 $options['locale'] 是作为选项注入到表单中的语言环境
您可以创建一个不同的 EntityType class 来扩展您的自定义 DoctrineType class 来初始化 PropertyAccessor 以支持魔法调用
有关 属性 访问器的更多信息:
http://symfony.com/doc/current/components/property_access/introduction.html
关于第二种方式: