Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] [User Error] Uncaught LogicException: GridField doesn't have a modelClassName, so it doesn't know the columns of this grid.


Go to End


3 Posts   1899 Views

Avatar
djpmedia

Community Member, 13 Posts

26 June 2015 at 1:28am

Edited: 26/06/2015 1:30am

I've extended the page class with some additional fields.

class Page extends SiteTree {

I've a few has_one relations and one of them is a image

private static $has_one = array(
		'BackgroundImage' => 'BackgroundImage',
		'BottomContentImage' => 'Image',
		'NavLevel2Image' => 'Image'
	);

For the CMS fields I have this:

 public function getCMSFields() {
    	$fields = parent::getCMSFields();
 $config = GridFieldConfig_RelationEditor::create();
            $gridField = new GridField('BackgroundImage', 'BackgroundImage', $this->BackgroundImage(), $config);
            $fields->addFieldToTab('Root.Images.Main',$gridField);
return $fields

Know I get an error for the BackgroundImage class.
[User Error] Uncaught LogicException: GridField doesn't have a modelClassName, so it doesn't know the columns of this grid.

For the info, the BackgroundImage dataobject looks like this:

<?php
class BackgroundImage extends DataObject {
	
	public static $db = array(
	);
	
	public static $has_one = array(
		'Image' => 'Image',
	);

    public static $summary_fields = array(
        'CMSThumbnail' => 'Thumbnail',
        'Title' => 'Title'
    );

    public static $casting = array(
        'Title' => 'Varchar(255)',
        'CMSThumbnail' => 'HTMLText'
    );
	
	public function updateCMSFields(FieldList $fields) {
		$fields->push(new UploadField('Image',_t('BackgroundImage.Image_Title','Image')));
		return $fields;
	}

    public function getTitle() {
        $title = '&nbsp;';
        if ((int) $this->ImageID > 0) {
                $img = DataObject::get_by_id('Image',$this->ImageID);
                if ($img) {
                    $title = $img->Filename;
                }
        }
        return $title;
     }

   public function getCMSThumbnail() {
        $rtnhtml = '&nbsp;';
        /**
         * @var Image $img
         */
        try {
            if ((int) $this->ImageID > 0) {
                $img = DataObject::get_by_id('Image',$this->ImageID);
                if ($img) {
                    $rtnhtml = $img->CMSThumbnail()->forTemplate();
                }
            }
        } catch (Exception $e) {
            $rtnhtml = '&nbsp;';
        }
        return $rtnhtml;
    }
	    
}

How can I thinks this error?

Avatar
Pyromanik

Community Member, 419 Posts

26 June 2015 at 1:40am

Edited: 26/06/2015 1:42am

GridField is for managing *_many relationships, which this is not.
Just use an UploadField (for a direct Image relation) or a DropdownField (to other objects).

Or look at http://addons.silverstripe.org/add-ons/simonwelsh/hasoneedit perhaps.

Avatar
djpmedia

Community Member, 13 Posts

26 June 2015 at 1:57am

Thanks, I've changed it to a UploadField and everything is working fine right know.