gpt4 book ai didi

Silverstripe 3 - GridFieldExtensions 多类添加

转载 作者:行者123 更新时间:2023-12-04 04:18:11 27 4
gpt4 key购买 nike

我正在尝试使用 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'
);

}

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

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

[User Error] Couldn't run query: 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 Column 'ModularPageID' in where clause is ambiguous

最佳答案

这是我通常如何设置我的 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'
);
//...
}

关于Silverstripe 3 - GridFieldExtensions 多类添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com