调用未定义的方法Silverstripe Gridfield

时间:2019-03-25 02:27:23

标签: php silverstripe-4

我需要在扩展文件上添加其他字段,但是发生错误。

这将添加带有GridField的图库选项卡,允许用户添加图像。

不知道缺少什么,因为它正在其他项目上工作。

[紧急]未捕获的错误:调用未定义的方法MyProject \ Extensions \ BlogPostExtension :: BlogGalleryImages()

<?php 


namespace MyProject\Extensions;


use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
use SilverStripe\View\Requirements;
use MyProject\Model\BlogGalleryImage;

class BlogPostExtension extends DataExtension {

    private static $db = [
        'SortOrder' => 'Int'
    ];

    private static $has_many = [
        "BlogGalleryImages" => BlogGalleryImage::class,
    ];

    public function updateCMSFields(FieldList $fields) {
        // Add fields here

        $fields->removeByName("SortOrder");
        $options = $this->BlogGalleryImages();
        $gridFieldConfig = GridFieldConfig::create()->addComponents(
                new GridFieldAddNewButton('toolbar-header-right'),
                new GridFieldToolbarHeader(),
                new GridFieldSortableHeader(),
                new GridFieldDataColumns(),
                new GridFieldPaginator(80),
                new GridFieldDetailForm(),
                new GridFieldEditButton(),
                new GridFieldDeleteAction()
        );
        $itemsTable = new GridField("BlogGalleryImages","Gallery Image",$options,$gridFieldConfig);
        $fields->addFieldToTab('Root.Gallery',$itemsTable);

    }

}

这是应该在BlogPostExtension类上调用的Dataobject文件

<?php 

namespace MyProject\Model;

use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\ORM\DataObject;

class BlogGalleryImage extends DataObject{

    private static $singular_name = 'Gallery Image';

    private static $db = array(
        'Title' => 'Varchar(256)',
        'SortOrder' => 'Int'
    );

    private static $has_one = array(
        'Image' => Image::class,
        'BlogPosts' => BlogPost::class,
    );

    private static $owns = [
        'Image',
    ];

    private static $summary_fields = array(
        'Thumbnail',
        'Title'
    );

    private static $table_name = 'MyProject_BlogGallery';


    private static $default_sort = "SortOrder ASC";

    public function getCMSFields(){
        $fields = parent::getCMSFields();

        $fields->removeByName("SortOrder");
        $fields->removeByName("BlogPostID");
        $fields->addFieldToTab("Root.Main", new TextField("Title","Title"));
        $fields->addFieldToTab("Root.Main", $uploadfield = UploadField::create("Image","Image"));
        $uploadfield->setFolderName("BlogGallery");

        return $fields;
    }

    public function getThumbnail(){
        if ($icon = $this->Image()){
            return $icon->CMSThumbnail();
        }else{
            return '(No Image)';
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您正在扩展中调用一个不存在的实例方法:

$options = $this->BlogGalleryImages();

这实际上是一个魔术方法,代表您的has_many关系“ BlogGalleryImages”-由DataObject支撑。您需要参考扩展的所有者才能访问它:

$options = $this->getOwner()->BlogGalleryImages();