Symfony2 中 SonataAdminBundle 列表的 toString 方法 [英] toString method for SonataAdminBundle Listing in Symfony2
问题描述
在 Symfony 2.3 中,我使用 SonataAdminBundle ( master ) 并且我试图让 ManyToMany 在列表中工作.问题是 SonataAdminBundle 要求使用 toString() 方法.将这个方法实现到相关的实体就解决了这个问题.
In Symfony 2.3 i am using SonataAdminBundle ( master ) and i am trying to get ManyToMany working in Listing. The Problem is that SonataAdminBundle is asking for a toString() method. Implementing this method to the related Entity solves the problem.
我的问题:我是否必须实现 toString 方法,或者是否有一个选项可以告诉 SonataAdminBundle 使用而不是调用 toString 方法的属性?
My Question: Do i have to implement the toString method or is there a Option to tell SonataAdminBundle a property for using instead of calling the toString method?
谢谢
推荐答案
据我所知,这是强制性的.
As far as I know, it's mandatory.
但是如果需要,您可以返回另一个属性值.此外,您可以防止自己在对象没有数据时尝试显示属性(例如,当您添加新对象"时)
But you can return another property value if you want. Also, you can prevent yourself from trying to display a property when the object has no data (for example, when you are "Adding a new object")
有一个简单的方法:
public function __toString()
{
return ($this->getName()) ? : '';
}
这篇关于Symfony2 中 SonataAdminBundle 列表的 toString 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!