【问题标题】:Silverstripe 3 - GridFieldExtensions MultiClass AddingSilverstripe 3 - GridFieldExtensions 多类添加
【发布时间】:2014-12-10 17:31:53
【问题描述】:

我正在尝试使用https://github.com/silverstripe-australia/silverstripe-gridfieldextensions/ 创建一个网格字段,我可以在其中添加不同类型的数据对象。

遗憾的是,我不知道如何在我想要网格字段的班级上编写正确的代码。

有人可以指点我正确的方向吗?

更新: 根据您的回答,我现在有以下结构

class ModularPage extends Page {

   private static $has_many = array(
    'Sections' => 'MP_Section',
    'Galleries' => 'MP_Gallery',
    'Paragraphs' => 'MP_Paragraph'
    );

    public function getCMSFields() {
        ...

        $fields->addFieldToTab('Root.Main', $mutli_grid = GridField::create('Sections', 'Sektionen', $this->Sections(), MultiClassGrid::create(15)));

        ...
    }

}

class MP_Section extends DataObject {

    private static $has_one = array(
        'Section' => 'MP_Section',
        'ModularPage' => 'ModularPage'
    );

}

class MP_Gallery extends MP_Section {

    private static $has_one = array(
        'Section' => 'MP_Section',
        'ModularPage' => 'ModularPage'
    );

}

到目前为止,还好吗?直到现在都这样吗?

原因如果我想添加一个画廊,我会收到以下错误

[用户错误] 无法运行查询:SELECT DISTINCT "MP_Section"."ID", "MP_Section"."SortID" FROM "MP_Section" LEFT JOIN "MP_Gallery" ON "MP_Gallery"."ID" = "MP_Section "."ID" LEFT JOIN "MP_Paragraph" ON "MP_Paragraph"."ID" = "MP_Section"."ID" WHERE ("ModularPageID" = '13') ORDER BY "MP_Section"."SortID" ASC LIMIT 9223372036854775807 列' where 子句中的 ModularPageID' 不明确

【问题讨论】:

  • 我更新了我的问题
  • 什么是MultiClassGrid?为什么所有的 DataObjects 都具有相同的 has_one 定义?只有MP_Section 应该有'ModularPage' => 'ModularPage',就这样?在页面上,你可能真的需要在 has_many 中的'Sections' => 'MP_Section',就是这样......
  • 谢谢,没用的 $has_one 关系是个愚蠢的错误。 MultiClassGrid 是一个扩展“GridFieldConfig”的类,以便我可以重用相同的配置

标签: silverstripe


【解决方案1】:

这是我通常如何设置我的GridField

$c = GridFieldConfig_RelationEditor::create();
$c->removeComponentsByType('GridFieldAddNewButton')
  ->addComponent(new GridFieldAddNewMultiClass())
  ;

$c->getComponentByType('GridFieldAddNewMultiClass')
  ->setClasses(array(
    'SectionThemesBlock'    => SectionThemesBlock::get_section_type(),
    'SectionFeaturedCourse' => SectionFeaturedCourse::get_section_type(),
    'SectionCallForAction'  => SectionCallForAction::get_section_type(),
    'SectionContactSheet'   => SectionContactSheet::get_section_type()
    //....
  ));

$f = GridField::create('Sections', "Sections", $this->Sections(), $c);
$fields->addFieldToTab("Root.Sections", $f);

基于GridFieldConfig_RelationEditor,只需删除GridFieldAddNewButton,然后添加GridFieldAddNewMultiClass。然后配置组件以了解要在下拉列表中创建哪些类。所有这些SectionThemesBlockSectionFeaturedCourse 等都扩展了一个通用的Section 数据对象作为基础。 get_section_type() 函数是 Section 数据对象上的自定义静态函数,可以在下拉列表中获得一个漂亮的名称,而不必一直手动键入它......

Section 数据对象的基本原理如下所示:

class Section extends DataObject {

  public static function get_section_type()
  {
    return trim(preg_replace('/([A-Z])/', ' $1', str_ireplace('Section', '', get_called_class())));
  }

  //...
}

以及该gridField所在的页面以及定义了关系的页面:

class Page extends SiteTree {
  //...
  private static $has_many = array(
    'Slides' => 'Slide'
  );
  //...
}

【讨论】:

  • 您好,感谢您的帮助。我使用了这个并更新了我的问题。
  • 我在上面的答案中添加了 Page 类。很可能,您的问题来自您如何定义您的关系。你应该只有一个像'Sections' => 'MP_Section'
【解决方案2】:

这样的东西应该可以工作

        $config = new GridFieldConfig_RecordEditor();
        $config->addComponent(new GridFieldAddNewMultiClass());
         ...
        $grid = GridField::create('Grid', 'Grid', $this->GalleryItems(), $config);

你需要三个数据对象:

  • GalleryItem extends DataObject{}
  • FooGalleryItem extends GalleryItem{}
  • BarGalleryItem extends GalleryItem{}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多