我无法删除 Silverstripe 中 $has_one 的默认下拉列表

I Can Not Remove The Default Drop Down of a $has_one in Silverstripe

我试过: removeFieldFromTab 移除名称 替换字段

但字段仍然存在。

use SilverStripe\ORM\DataObject;
use //.....

class Product extends DataObject {
    private static $db = [
        'ProductName'=>'Varchar',
        'TagLine'=>'Text',
        'GeneralDescription'=>'HTMLText'

    ];
    private static $has_one = [
        'SplashImage'=>Image::Class,
        'ProductCategory'=>ProductCategory::Class
    ];
    private static $has_many = [
        'ProductImage'=>Image::Class,
        'Features'=>'Feature'
    ];
    private static $owns = [
        'SplashImage',
        'ProductImage'
    ];
    private static $summary_fields = array(
        'ProductName'=>'Product Name'
    );   
    private static $searchable_fields = [

    ];
    public function getCMSFields(){
        $fields = parent::getCMSFields();            

        $categoryField = DropdownField::create('ProductCategory', 'Choose Product Category', ProductCategory::get()->map('ID', 'ProductCategoryTitle'));            

        $fields->replaceField('ProductCategory', $categoryField);

        return $fields;     
    }
}

我没有收到任何错误,但 ID 为 # 的默认下拉字段位于顶部。

对于 has_one 关系,字段名称应为 <RelationName>ID,因此在您的情况下 ProductCategoryID.

您必须将附加 ID 引用到关系名称,以便 SilverStripe 按名称从 CMS 选项卡中删除该字段。

$fields->removeByName('ProductCategoryID');

此外,如果您为 has_one 关系创建自定义字段,请确保使用 <RelationName>ID 作为该字段的名称。例如。要创建下拉菜单,请使用:

DropdownField::create(
    'ProductCategoryID', // Use RelationName + ID
    'Choose Product Category', 
    ProductCategory::get()->map('ID', 'ProductCategoryTitle')
);