SilverStripe 4 beta3 TreeDropdownField给出错误:SourceObject必须是DataObject的子类

时间:2017-09-26 15:26:04

标签: php silverstripe silverstripe-4

我必须遗漏一些简单的...我正在使用这里的文档:http://api.silverstripe.org/en/master/class-SilverStripe.Forms.TreeDropdownField.html我认为我正确地做了这个,但是得到错误:SourceObject必须是DataObject的子类

这是我的代码

<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;
class HomePage extends Page {
    private static $db = array(
        'CTA'       => 'Varchar(255)',
        'LinkText'  => 'Varchar(50)'
    );
    private static $has_one = array(
        'PageLink'  => 'SiteTree'

    );
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', TextareaField::create('CTA', 'Call To Action Text'), 'Content');
        $fields->addFieldToTab('Root.Main', TreeDropdownField::create('PageLinkID', 'What page does this call-to-action link to?', 'SiteTree'), 'Content');
        $fields->addFieldToTab('Root.Main', TextField::create('PageLinkText', 'Call To Action Link Text (Example: "Learn more")'), 'Content');
        $fields->removeByName('Content');
        return $fields;
    }
}

1 个答案:

答案 0 :(得分:3)

您需要使用完全限定的类名:

$fields->addFieldToTab(
    'Root.Main',
    TreeDropdownField::create(
        'PageLinkID',
        'What page does this call-to-action link to?',
        SiteTree::class
    ),
    'Content'
);

这也适用于你的模特关系:

private static $has_one = array(
    'PageLink'  => SiteTree::class,

);