简体   繁体   English

Silverstripe将数据对象添加到Siteconfig

[英]Silverstripe add dataobject to Siteconfig

I am trying to add a dataobject to the site config (Settings). 我正在尝试将数据对象添加到站点配置(设置)中。 I have managed to add a logo field and this works now I would like to add some repeater fields eg (social media fields). 我设法添加了徽标字段,并且现在可以使用了,例如,我想添加一些转发器字段(社交媒体字段)。 This doesn't work. 这行不通。 I am getting an error. 我收到一个错误。

Code and error below. 下面的代码和错误。 I have looked in the database and all the fields have been created after the dev/build. 我查看了数据库,并在dev / build之后创建了所有字段。 I have also Flush=all. 我也有Flush = all。

mysite/_config/app.yml mysite的/ _config / app.yml

SiteConfig:
 extensions:
  - CustomSiteConfig

mysite/code/extensions/CustomSiteConfig.php mysite的/代码/扩展/ 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 mysite的/代码/扩展/ 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')

        );

    }
}

Error Message 错误信息

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

Line 34 is 第34行是

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

On a decorator/DataExtension one should use $this->owner instead of just $this. 在装饰器/ DataExtension上,应该使用$ this-> owner而不是$ this。 Line 34 should look like this: 第34行应如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM