我可以在 Silverstripe 4 的管理页面中有 2 个网格字段组件吗

Can I have 2 gridfield components in an admin page in Silverstripe 4

我可以在管理页面中使用与同一个数据对象不同的名称的 2 个网格字段组件吗 - 例如

   class MainLandingPage_au extends Page
   {

    private static $has_many = [
       'ImagesWithHtml' => ImageWithHtml::class,
       'ImagesWithHtml2' => ImageWithHtml::class,
    ];

    // ...
    $fields->addFieldToTab('Root.Section1', HtmlEditorField::create('Section1Title','Section 1 Title')->setRows(4));
    $fields->addFieldToTab('Root.Section1', GridField::create(
        'ImagesWithHtml',
        'Images With Html For This Page',
        $this->ImagesWithHtml(),
        GridFieldConfig_RecordEditor::create()
    ));

    $fields->addFieldToTab('Root.Section2', HtmlEditorField::create('Section2Title','Section 2 Title')->setRows(4));
    $fields->addFieldToTab('Root.Section2', GridField::create(
        'ImagesWithHtml2',
        'Images With Html For Section 2',
        $this->ImagesWithHtml2(),
        GridFieldConfig_RecordEditor::create()
    ));

你试过了吗?由于你有不同的关系,它应该在一般情况下工作。当然你需要ImageWithHTMLclass上对应的has_one关系,见https://docs.silverstripe.org/en/4/developer_guides/model/relations/#has-many

所以你的代码应该是这样的:

   class MainLandingPage_au extends Page
   {

    private static $has_many = [
       'ImagesWithHtml' => `\Namespace\Of\ImageWithHtml.Foo`,
       'ImagesWithHtml2' => `\Namespace\Of\ImageWithHtml.Bar`
    ];

另一边

   class ImageWithHtml extends DataObject
   {

    private static $has_one = [
       'Foo' => MainLandingPage_au::class,
       'Bar' => MainLandingPage_au::class
    ];

是的,可以使用 2 个网格区域,这对正确的个体关系有效

class MainLandingPage_au extends Page
   {

    private static $has_many = [
       'ImagesWithHtml' => ImageWithHtml::class . '.AuMainLandingPage',
       'ImagesWithHtml2' => ImageWithHtml::class . '.AuMainLandingPage2'
    ];

    // ...
    // Gridfields as in posted question
    // ... etc

另一边

class ImageWithHtml extends DataObject
    {

    private static $has_one = [
       'AuMainLandingPage' => AuMainLandingPage::class . '.ImagesWithHtml',
       'AuMainLandingPage2' => AuMainLandingPage::class . '.ImagesWithHtml2'
    ];