Silverstripe 3.4.0 - 设置 DataObject 和页面之间的关系 - 数据不保存到数据库
Silverstripe 3.4.0 - Set Relationship between DataObject and Page - Data not saving to database
我有一个带有项目库的网站,该网站上有专用于项目类型的子页面。我想让内容管理员能够 select 任何项目库项目显示在他们选择的任何子页面上。一个子页面可以有多个项目库项目,一个项目库项目可以显示在一个或多个子页面上。
我在 Page.php
上设置了一个 CheckboxSetField
,它显示 ProjectGalleryItem
DataObject 中所有项目名称的列表。我的复选框列表显示在所有子页面的单独选项卡中,但我无法获得任何 selected 值来保存——选中的复选框在保存时会自动取消选中。日志文件或控制台中未显示任何错误。
我想知道 Page
和 ProjectGalleryItem
之间的关系是否不正确?
这是 ProjectGalleryItem
的代码:
<?php
class ProjectGalleryItem extends DataObject
{
private static $db = array(
'ProjectName' => 'Varchar(250)',
'ProjectDescription' => 'Text',
);
private static $has_one = array(
'ProjectImage' => 'Image',
);
private static $belongs_many_many = array(
'Pages' => 'Page',
);
private static $summary_fields = array(
'ProjectName',
'ProjectDescription',
);
}
class ProjectGalleryItemAdmin extends ModelAdmin {
private static $managed_models = 'ProjectGalleryItem';
private static $url_segment = 'ProjectGalleryItems';
private static $menu_title = 'Project Gallery Items';
}
这是 Page.php
文件的一半:
<?php
class Page extends SiteTree
{
private static $db = array(
'BannerH1' => 'HTMLText',
'BannerSubH1' => 'HTMLText',
'H1' => 'varchar(250)',
'PageTitle' => 'varchar(250)',
'BottomContent' => 'HTMLText',
'StylizedList' => 'HTMLText',
);
private static $has_one = array(
'BannerImg' => 'Image',
);
private static $has_many = array(
'RotatorImages' => 'RotatorImage',
);
private static $many_many = array(
'ProjectGalleryItems' => 'ProjectGalleryItem'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");
$fields->addFieldToTab("Root.Main", new HTMLEditorField("BannerH1"), "Content");
$fields->addFieldToTab("Root.Main", new HTMLEditorField("BannerSubH1"), "Content");
$fields->addFieldToTab("Root.Main", new HTMLEditorField('BottomContent', "Bottom Content"));
$fields->addFieldToTab("Root.Main", new HTMLEditorField('StylizedList', "Stylized List"), "BottomContent");
$fields->addFieldToTab('Root.Main', new TextField('PageTitle', 'Page Title'), 'MetaDescription');
$fields->addFieldToTab("Root.Main", new UploadField('BannerImg', "Subpage Banner"), "Content");
$metadataTab = $fields->findOrMakeTab('Root.Main.Metadata');
$fields->removeByName('Metadata');
$fields->addFieldToTab('Root.Main', $metadataTab);
$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkUpload());
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
// field from drawer class => label in UI
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail',
'InternalURL.Link' => 'Internal URL',
));
$gridfield = new GridField(
"RotatorImages",
"Rotator Images",
$this->RotatorImages()->sort("SortOrder"),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Rotator Images', $gridfield);
$projects = DataObject::get('ProjectGalleryItem');
if(!empty($projects))
{
$map = $projects->Map('ID', 'ProjectName');
$fields->addFieldToTab('Root.Projects',
CheckboxSetField::create(
$name = "Projects",
$title = "Select Projects",
$source = $map
));
}
return $fields;
}
}
在我看来,问题出在您的表单字段名称上,它应该与关系名称相匹配。 CheckboxsetField::create()
的第一个参数应该是 ProjectGalleryItems
,而不是 Projects
。
我有一个带有项目库的网站,该网站上有专用于项目类型的子页面。我想让内容管理员能够 select 任何项目库项目显示在他们选择的任何子页面上。一个子页面可以有多个项目库项目,一个项目库项目可以显示在一个或多个子页面上。
我在 Page.php
上设置了一个 CheckboxSetField
,它显示 ProjectGalleryItem
DataObject 中所有项目名称的列表。我的复选框列表显示在所有子页面的单独选项卡中,但我无法获得任何 selected 值来保存——选中的复选框在保存时会自动取消选中。日志文件或控制台中未显示任何错误。
我想知道 Page
和 ProjectGalleryItem
之间的关系是否不正确?
这是 ProjectGalleryItem
的代码:
<?php
class ProjectGalleryItem extends DataObject
{
private static $db = array(
'ProjectName' => 'Varchar(250)',
'ProjectDescription' => 'Text',
);
private static $has_one = array(
'ProjectImage' => 'Image',
);
private static $belongs_many_many = array(
'Pages' => 'Page',
);
private static $summary_fields = array(
'ProjectName',
'ProjectDescription',
);
}
class ProjectGalleryItemAdmin extends ModelAdmin {
private static $managed_models = 'ProjectGalleryItem';
private static $url_segment = 'ProjectGalleryItems';
private static $menu_title = 'Project Gallery Items';
}
这是 Page.php
文件的一半:
<?php
class Page extends SiteTree
{
private static $db = array(
'BannerH1' => 'HTMLText',
'BannerSubH1' => 'HTMLText',
'H1' => 'varchar(250)',
'PageTitle' => 'varchar(250)',
'BottomContent' => 'HTMLText',
'StylizedList' => 'HTMLText',
);
private static $has_one = array(
'BannerImg' => 'Image',
);
private static $has_many = array(
'RotatorImages' => 'RotatorImage',
);
private static $many_many = array(
'ProjectGalleryItems' => 'ProjectGalleryItem'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");
$fields->addFieldToTab("Root.Main", new HTMLEditorField("BannerH1"), "Content");
$fields->addFieldToTab("Root.Main", new HTMLEditorField("BannerSubH1"), "Content");
$fields->addFieldToTab("Root.Main", new HTMLEditorField('BottomContent', "Bottom Content"));
$fields->addFieldToTab("Root.Main", new HTMLEditorField('StylizedList', "Stylized List"), "BottomContent");
$fields->addFieldToTab('Root.Main', new TextField('PageTitle', 'Page Title'), 'MetaDescription');
$fields->addFieldToTab("Root.Main", new UploadField('BannerImg', "Subpage Banner"), "Content");
$metadataTab = $fields->findOrMakeTab('Root.Main.Metadata');
$fields->removeByName('Metadata');
$fields->addFieldToTab('Root.Main', $metadataTab);
$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkUpload());
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
// field from drawer class => label in UI
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail',
'InternalURL.Link' => 'Internal URL',
));
$gridfield = new GridField(
"RotatorImages",
"Rotator Images",
$this->RotatorImages()->sort("SortOrder"),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Rotator Images', $gridfield);
$projects = DataObject::get('ProjectGalleryItem');
if(!empty($projects))
{
$map = $projects->Map('ID', 'ProjectName');
$fields->addFieldToTab('Root.Projects',
CheckboxSetField::create(
$name = "Projects",
$title = "Select Projects",
$source = $map
));
}
return $fields;
}
}
在我看来,问题出在您的表单字段名称上,它应该与关系名称相匹配。 CheckboxsetField::create()
的第一个参数应该是 ProjectGalleryItems
,而不是 Projects
。