【问题标题】:I Can Not Remove The Default Drop Down of a $has_one in Silverstripe我无法删除 Silverstripe 中 $has_one 的默认下拉菜单
【发布时间】:2017-12-29 23:51:27
【问题描述】:

我尝试过: removeFieldFromTab removeByName 替换字段

但该字段仍然存在。

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

【问题讨论】:

  • 对于重新排序,您可以使用changeFieldOrder
  • 我不需要重新订购。我需要删除默认字段并使用我自己的。

标签: silverstripe silverstripe-4


【解决方案1】:

对于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')
);  

【讨论】:

  • 这不在文档中并且正在做:'ProductCategory'=>'ProductCategoryID' 引发错误
  • 我从另一个答案中明白了你的意思。我在 removeByName 方法中需要“ProductCategoryID”。您能否编辑您的答案,以便我将其标记为正确。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
相关资源
最近更新 更多