gpt4 book ai didi

symfony - Symfony2 中 SonataAdminBundle 列表的 toString 方法

转载 作者:行者123 更新时间:2023-12-04 23:23:38 27 4
gpt4 key购买 nike

在 Symfony 2.3 中,我使用 SonataAdminBundle ( master ) 并且我试图让 ManyToMany 在列表中工作。问题是 SonataAdminBundle 要求使用 toString() 方法。将这个方法实现到相关的 Entity 解决了这个问题。

我的问题:我是否必须实现 toString 方法,或者是否有一个选项可以告诉 SonataAdminBundle 使用而不是调用 toString 方法的属性?

谢谢

最佳答案

据我所知,这是强制性的。

但是如果需要,您可以返回另一个属性值。此外,您可以防止自己在对象没有数据时尝试显示属性(例如,当您“添加新对象”时)

有一个简单的方法:

public function __toString()
{
return ($this->getName()) ? : '';
}

关于symfony - Symfony2 中 SonataAdminBundle 列表的 toString 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17545489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com