我正在尝试使用https://github.com/silverstripe-australia/silverstripe-gridfieldextensions/创建一个gridfield,可以在其中添加不同类型的数据对象。
可悲的是,我无法弄清楚如何在需要gridfield的类上为此编写正确的代码。
有人能指出我正确的方向吗?
更新:
根据您的回答,我现在具有以下结构
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”来自“ MP_Section”左联接“ MP_Gallery” ON“ MP_Gallery”。“ ID” =“ MP_Section”。“ ID“左联接” MP_Paragraph“到” MP_Paragraph“上。” ID“ =” MP_Section“。” ID“ WHERE(” ModularPageID“ ='13')ORDER BY” MP_Section“。” SortID“ ASC LIMIT 9223372036836854775807列'ModularPageID'在where子句不明确
最佳答案
这是我通常设置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
。然后配置组件以知道要创建的下拉列表中有哪些可用类。所有这些SectionThemesBlock
,SectionFeaturedCourse
等都扩展了通用的Section
dataObject作为基础。 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 - Silverstripe 3-GridFieldExtensions多类添加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27407274/