gpt4 book ai didi

silverstripe - 无法在 SilverStripe 4.4.4 中自定义 ModelAdmin 的过滤器/搜索形式

转载 作者:行者123 更新时间:2023-12-05 07:16:46 26 4
gpt4 key购买 nike

我正在从事 SilverStripe 项目。基本上,我将我的项目更新到 SilverStripe 版本 4.4.4。升级后,我发现 ModelAdmin 的搜索/过滤形式发生了变化,如下图所示。

enter image description here

我现在要做的是,在本课之后,我正在尝试自定义 ModelAdmin 的搜索/过滤表单的字段。 https://www.silverstripe.org/learn/lessons/v4/introduction-to-modeladmin-1 .

我有一个链接到模型管理类的数据对象类。以下是模型管理类的伪代码。

class EnquirySubmission extends DataObject
{
private static $db = [
//some hidden fields are here
];

private static $has_one = [
'Member' => Member::class
];

private static $summary_fields = [
'Member.Name' => 'Member',
//some hidden fields are here
];

//some hidden code goes here

public function searchableFields()
{
return [
'Member.Name' => [
'filter' => 'PartialMatchFilter',
'title' => 'Member',
'field' => \SilverStripe\Forms\DropdownField::create('Member.Name')
->setSource(
Member::get()->map('ID','Email')
)->setEmptyString('-- Member --')
],
];
}
}

如您在代码中所见,我通过重写 searchableFields 方法自定义过滤器/搜索表单。但它不适用于升级版的 SilverStripe。我缺少什么,我该如何解决?

最佳答案

Silverstripe 和 ModelAdmin 是王牌,但令人困惑的是为什么到目前为止这个日期范围搜索问题需要在每个版本中进行调整。这是一个完整的示例,我刚刚开始使用 4.7.2(发布时的最新稳定版本)...

app/src/Test/MyDataObject.php

namespace MyVendor\MyNamespace;

use SilverStripe\Forms\DateField;
use SilverStripe\ORM\DataObject;

class MyDataObject extends DataObject {

private static $db = [
'Title' => 'Varchar',
'MyDateTimeField' => 'DBDatetime'
];

private static $summary_fields = ['Title','MyDateTimeField'];

public function updateAdminSearchFields($fields) {
$fields->removeByName('MyDateTimeField');//needed as added in summary field
$fields->push(DateField::create('MyDateTimeField:GreaterThanOrEqual', 'MyDateTimeField (Start)'));
$fields->push(DateField::create('MyDateTimeField:LessThanOrEqual', 'MyDateTimeField (End)'));
}
}

app/src/Test/MyAdmin.php

namespace MyVendor\MyNamespace;

use SilverStripe\Admin\ModelAdmin;

class MyAdmin extends ModelAdmin {

private static $menu_title = 'MyAdmin';
private static $url_segment = 'myadmin';
private static $managed_models = [MyDataObject::class];

public function getList() {
$list = parent::getList();
if ($params = $this->getRequest()->requestVar('filter'))
if ($filters = $params[$this->sanitiseClassName($this->modelClass)])
return $list->filter($filters);

return $list;
}
}

app/src/Test/MyAdminExtension.php

namespace MyVendor\MyNamespace;

use SilverStripe\ORM\DataExtension;

class MyAdminExtension extends DataExtension {
public function updateSearchContext($context) {
$class = $context->getQuery([])->dataClass();
if (method_exists($class, 'updateAdminSearchFields'))
(new $class)->updateAdminSearchFields($context->getFields());

return $context;
}
}

app/_config/mysite.yml

MyVendor\MyNamespace\MyAdmin:
extensions:
- MyVendor\MyNamespace\MyAdminExtension

关于silverstripe - 无法在 SilverStripe 4.4.4 中自定义 ModelAdmin 的过滤器/搜索形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59092117/

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