Silverstripe将dataobject添加到Siteconfig

时间:2015-02-04 14:14:32

标签: php silverstripe

我正在尝试将数据对象添加到站点配置(设置)。  我已经设法添加了一个徽标字段,现在我可以添加一些转发器字段,例如(社交媒体字段)。这不起作用。我收到了一个错误。

下面的代码和错误。我查看了数据库,并且在dev / build之后创建了所有字段。 我也有Flush = all。

mysite的/ _config / app.yml

SiteConfig:
 extensions:
  - CustomSiteConfig

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的/代码/扩展/ 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);

1 个答案:

答案 0 :(得分:4)

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

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

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