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

问题描述 投票:1回答:1

我试过:removeFieldFromTab removeByName replaceField

但该领域仍然存在。

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#的默认下拉字段位于顶部。

silverstripe silverstripe-4
1个回答
5
投票

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')
);  
© www.soinside.com 2019 - 2024. All rights reserved.