GridField中的通用布尔格式

时间:2016-06-01 09:35:51

标签: php silverstripe

对于布尔字段,摘要默认将值显示为0或1 ...

class MyDataObject extends DataObject {

    private static  $db = array(
        'Name'      => 'Varchar(255)',
        'MyBoolean' => 'Boolean',
    );

    public static $summary_fields = array (
        'Name',
        'MyBoolean',
    );
}

......我知道如何使用强制转换将其更改为是或否...

class MyDataObject extends DataObject {

    private static  $db = array(
        'Name'      => 'Varchar(255)',
        'MyBoolean' => 'Boolean',
    );

    public static $casting = array (
        'MyBooleanText' => 'Text',
    );

    public function MyBooleanText() {
        return $this->MyBoolean ? 'Yes' : 'No';
    }

    public static $summary_fields = array (
        'Name',
        'MyBooleanText',
    );
}

...但是我的问题是如何默认更改每个值以在摘要行中显示它而不必为每个对象设置转换?

1 个答案:

答案 0 :(得分:1)

在你的$summary_fields中你只需要将.Nice方法添加到你的布尔值。

E.g:

class MyDataObject extends DataObject {

    static  $db = array(
        'Name'      => 'Varchar(255)',
        'MyBoolean' => 'Boolean',
    );

    public static $summary_fields = array (
        'Name' => 'Name',
        'MyBoolean.Nice' => 'MyBoolean',
    );
}