SonataAdminBundle 中的选项使用 configureListFields

Option usage configureListFields in SonataAdminBundle

不幸的是,当您向列表中添加字段时,文档并未涵盖如何使用 configureListFields ListMapper 中可用的选项。

这是我的基本代码

$listMapper
    ->add('myField', null, array(
        'label' => LabelHelper::LABEL_MY_FIELD,
        'code' => // what should I put here ... $this->methodName() is not working
    ))

我想使用 'code' 选项(docs - 第 7.2.1 节),因为我只想自定义一个字段的最终显示。我不想重写行模板。

如代码部分所述,我尝试了 returns 字符串的简单方法,但列表视图中没有任何反应(我已清除缓存等)。

答案很简单。您只需将方法名称不带括号

$listMapper
->add('myField', null, array(
    'label' => LabelHelper::LABEL_MY_FIELD,
    'code' => 'methodName'
))

方法应该存储在相应的实体中class