gpt4 book ai didi

php - Silverstripe 将数据对象添加到 Siteconfig

转载 作者:行者123 更新时间:2023-12-05 03:13:53 24 4
gpt4 key购买 nike

我正在尝试将数据对象添加到站点配置(设置)。 我已经设法添加了一个 Logo 字段,现在可以使用了我想添加一些转发器字段,例如(社交媒体字段)。这是行不通的。我遇到错误。

下面的代码和错误。我查看了数据库,所有字段都是在开发/构建之后创建的。我也有 Flush=all。

我的网站/_config/app.yml

SiteConfig:
extensions:
- CustomSiteConfig

mysite/code/extensions/CustomSiteConfig.php

class CustomSiteConfig extends DataExtension {

private static $has_one = array(
'Logo' => 'Image'
);

private static $has_many = array(
'SocialMedia' => 'SocialMedia'
);


public function updateCMSFields(FieldList $fields) {

$fields->addFieldToTab("Root.Main", new UploadField('Logo', 'Logo'));
$fields->addFieldToTab('Root.Main', $this->getSocialMedia());

}

protected function getSocialMedia() {
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm(),
new GridFieldSortableRows('SortID')
);

$gridField = new GridField("SocialMedia", "SocialMedia", $this->SocialMedia(), $gridFieldConfig);

return $gridField;
}

}

mysite/code/extensions/SocialMedia.php

class SocialMedia extends DataObject { 

public static $db = array(
'SortID' => 'Int',
'Social' => 'Text',
'Icon' => 'Text',
'URL' => 'Text'
);

public static $has_one = array(
'CustomSiteConfig' => 'CustomSiteConfig'
);

public function getCMSFields() {
return new FieldList(
new TextField('Social'),
new TextField('Icon'),
new TextField('URL')

);

}
}

错误信息

Fatal error: Call to undefined method CustomSiteConfig::SocialMedia() in file/path/mysite/code/extensions/CustomSiteConfig.php on line 34

第34行是

$gridField = new GridField("SocialMedia", "SocialMedia", $this->SocialMedia(), $gridFieldConfig);

最佳答案

在装饰器/DataExtension 上应该使用 $this->owner 而不是 $this。第 34 行应如下所示:

$gridField = new GridField("SocialMedia", "SocialMedia", $this->owner->SocialMedia(), $gridFieldConfig);

另请参阅:http://doc.silverstripe.org/en/developer_guides/extending/extensions#owner

关于php - Silverstripe 将数据对象添加到 Siteconfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323554/

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