除了定义 __toString() 之外,您如何定义要在 CRUD 形式中使用的 getter? [英] How do you define the getter to use in a CRUD form besides defining __toString()?

查看:30
本文介绍了除了定义 __toString() 之外,您如何定义要在 CRUD 形式中使用的 getter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用 Symfony2 的生成器从数据库实体创建 CRUD 表单,您可能会在创建新记录"屏幕上出现这样的错误:

If you've used Symfony2's generators to create CRUD forms from database entities, you may wind with an error like this on the "create new record" screen:

StringCastException: A "__toString()" method was not found on the objects of type
"ScrumBoard\ServiceBundle\Entity\Users" passed to the choice field. To read a
custom getter instead, set the option "property" to the desired property path.

如果我没看错,问题是它需要显示我正在创建的记录的用户下拉列表,但它不知道如何将用户"实体转换为字符串.

If I'm reading this correctly, the problem is that it needs to display a dropdown list of users for the record I'm creating, but it doesn't know how to turn a "User" entity into a string.

在我的用户实体类上定义 __toString() 方法解决了这个问题.但是,我可以从错误消息的文本中看到有一个替代方法:改为读取客户 getter,这是通过[设置] 选项属性"到所需的属性路径来实现的."

Defining the __toString() method on my Users entity class fixes the problem. However, I can see right from the text of the error message that there is an alternative: read a customer getter instead, which is accomplished by "[setting] the option "property" to the desired property path."

这听起来像是某种注释.但在我的搜索中,我无法弄清楚那是什么.因为我想彻底了解Symfony2--有人可以帮我吗?

This sounds like some kind of annotation. But in my searching, I can't figure out what that is. Because I want to have a thorough understanding of Symfony2--can someone help me out?

谢谢!

推荐答案

在表单中创建实体(选择的超类)字段类型时.您需要指定标签/值应使用哪个属性,否则将使用底层对象的 __toString() 方法.

When creating an entity ( superclass of choice ) field type in a form. You need to specify which property shall be used for the labels/values otherwise the __toString() method of the underlying object will be used.

$builder->add('users', 'entity', array(
    'class' => 'AcmeHelloBundle:User',
    'property' => 'username',
));

实体字段类型的表单类型参考中阅读更多相关信息.

Read more about it in the Form Type Reference for entity field Type.

附加信息

在模板中生成路由时,与 __toString() 相关的错误通常也来自 twig.如果使用 {{ object }} 输出一个对象,twig 中的一个对象 ... twig 将调用该对象的 __toString 方法.使用 SensioGeneratorBundle 生成的 crud 模板使用了这个技巧".

A __toString() related error generally often comes from twig when generating a route in a template aswell. if outputting an object an object in twig with {{ object }} ... twig will call the object'S __toString method. This "trick" is used by the crud generated templates with SensioGeneratorBundle.

 {{ path('article_show', {'id': article}) }}

路线是这样的:

article_show:
   pattern:  /article/{id}
   defaults: { _controller: AcmeArticleBundle:Article:show }

如果您的文章实体中的 __toString 方法设置为类似 ...

If you have the __toString method in your Article entity set to something like ...

 public function __toString()
 {
     return $this->id;
 }

...你不需要输入

{{ path('article_show', {'id': article.id) }}

一般Twig会自动输出Article::getId()如果你使用

Generally Twig will automatically output Article::getId() if you use

{{ article.id }}

希望这能澄清您的发现.

Hope this clarifies your findings.

这篇关于除了定义 __toString() 之外,您如何定义要在 CRUD 形式中使用的 getter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆