【问题标题】:Overriding getCustomSearchContext is not working in silverstripe覆盖 getCustomSearchContext 在 silverstripe 中不起作用
【发布时间】:2014-06-07 07:15:24
【问题描述】:

我创建了一个AlbumDataObject,如下,通过ModelAdmin在cms中管理。

在搜索过滤器中,Name 是唯一的输入字段。我还想显示一个Author 输入字段。

所以我试图覆盖getCustomSearchContext() 函数,但这不起作用。

class Album extends DataObject {

    private static $db = array(
        'Name' => 'Varchar(200)',
        'Author' => 'Varchar(200)',
    );

    private static $has_many = array(
        'Genres' => 'Genre'
    );


    public function getCustomSearchContext() {
        $fields = $this->scaffoldSearchFields(array(
            'restrictFields' => array()
        ));

        $filters = array(
            'Name' => new PartialMatchFilter('Name'),
            'Author' => new PartialMatchFilter('Author')
        );

        return new SearchContext(
            $this->class,
            $fields,
            $filters
        );
    }

}

我知道我们可以使用$searchable_fields,但我不想使用它们,因为我想在搜索表单中自定义表单字段。

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

    在 Silverstripe 3.1.5 中,getCustomSearchContext() 似乎不再存在。而是使用getDefaultSearchContext()

    我还创建了一个FieldList 并将字段推入。

    public function getDefaultSearchContext() {
        $fields = new FieldList();
        $fields->push(new TextField('Name', 'Name'));
        $fields->push(new TextField('Author', 'Author'));
    
        $filters = array(
            'Name' => new PartialMatchFilter('Name'),
            'Author' => new PartialMatchFilter('Author')
        );
    
        return new SearchContext(
            $this->class,
            $fields,
            $filters
        );
    }
    

    【讨论】:

    • 您好,如果您不忙,请问;你是怎么找到答案的
    • 嗨,托马斯。好问题。在检查了文档和各种测试之后,我检查了 Silverstripe api 中的 DataObject 类。我注意到没有getCustomSearchContext() 函数,但有一个getDefaultSearchContext() 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2020-02-14
    相关资源
    最近更新 更多