duangu4943
2015-02-04 14:14
浏览 23
已采纳

Silverstripe将dataobject添加到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. I have also Flush=all.

mysite/_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')

        );

    }
}

Error Message

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

Line 34 is

$gridField = new GridField("SocialMedia", "SocialMedia", $this->SocialMedia(), $gridFieldConfig);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshan8953 2015-02-04 14:47
    已采纳

    On a decorator/DataExtension one should use $this->owner instead of just $this. Line 34 should look like this:

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

    See also: http://doc.silverstripe.org/en/developer_guides/extending/extensions#owner

    点赞 打赏 评论

相关推荐 更多相似问题