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.

Customising the CMS /

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

Fatal error when trying to use GridField


Go to End


3 Posts   3806 Views

Avatar
oscarw_89

Community Member, 4 Posts

23 July 2012 at 2:26am

Hi!

I've been trying to get the GeidField to work for my project, but I always end up with an error..
I'm new to Silverstripe so I probobly just made a mistake somewhere..

Well, my problem is that I get this error message:

Fatal error: Call to a member function getName() on a non-object in ...\framework\forms\FieldList.php on line 405

I have a page extention where I want to have a grid field to add childs..

- - PlayerPage.php - -

<?php
class PlayerPage extends Page {
	static $description = "Video player page";
	static $db = array(
		"VideoURL" => "Varchar",
		"SpeakerVideoURL" => "Varchar"
  	);

	static $has_one = array(
		"Sidebar" => "WidgetArea",
		'VideoPhoto' => 'Image',
		'SpeakerVideoPhoto' => 'Image',
		'Video' => 'Video',
		'SpeakerVideo' => 'Video'
	);
	
	static $has_many = array(
		"Chapters" => "Chapter"
	);
	
	public function getCMSFields() {
		$fields = parent::getCMSFields();
		
		/* Video tab */
		$fields->addFieldToTab("Root.Video", new TextField('VideoURL', 'URL to main video'));
		$fields->addFieldToTab("Root.Video", new UploadField('Video', 'Main video'));
		$fields->addFieldToTab("Root.Video", new UploadField('VideoPhoto', 'Poster image for main video'));
		$fields->addFieldToTab("Root.Video", new TextField('SpeakerVideoURL', 'URL to speaker video'));
		$fields->addFieldToTab("Root.Video", new UploadField('SpeakerVideo', 'Speaker video'));
		$fields->addFieldToTab("Root.Video", new UploadField('SpeakerVideoPhoto', 'Poster image for speaker video'));
		
		/* Chapters tab */
		
		$itemsInGrid = $this->Chapters();
		//$itemsInGrid = DataList::create('Chapter'); 
				
		$gridColumns = new GridFieldDataColumns();
		
		$gridColumns->setDisplayFields(array(
			'Name' => 'Name',
			'Type' => 'Type',
			'StartTime' => 'Start time',
			'EndTime' => 'End time'		
		));	
		
		$gridFieldConfig = GridFieldConfig::create()->addComponents( 
		new GridFieldToolbarHeader(), 
		new GridFieldAddNewButton('toolbar-header-right'), 
		new GridFieldSortableHeader(), 
		//new GridFieldDataColumns(), 
		new GridFieldPaginator(10), 
		new GridFieldEditButton(), 
		new GridFieldDeleteAction(), 
		new GridFieldDetailForm() ,
		$gridColumns 
		);
		
		$gridField = new GridField("Chapters", "Chapters", $itemsInGrid, $gridFieldConfig);
		
		$fields->addFieldToTab("Root.Chapters", $GridField);
		
		/* Widgets tab */
		$fields->addFieldToTab("Root.Widgets", new WidgetAreaEditor("Sidebar"));

		return $fields;
	}
	
}
class PlayerPage_Controller extends Page_Controller {

}

- - Chapters.php - -

<?php
class Chapter extends DataObject {
	static $db = array(
		"Name" => "Varchar",
		"Type" => "Varchar",
		"StartTime" => "Decimal",
		"EndTime" => "Decimal"
	);

	static $has_one = array(
		"Player" => "PlayerPage"
	);
	
	static $summary_fields = array( 
		'Name' => 'Name',
		'Type' => 'Type', 
		'StartTime' => 'Start time', 
		'EndTime' => 'End time' 
	); 
	
	public function getName() {
		return "Chapter";
	}
	
	public function getCMSFields() {
		$fields = parent::getCMSFields();
		return $fields;
	}
}

Sense the error says that it's calling a non-object I'm guessing that "$this->Chapters();" isn't geting the chapters..

Thanks in advance for any help that could put me back on track again.

//Oscar

Avatar
jak

Community Member, 46 Posts

25 July 2012 at 8:22pm

The problem might be that you have a typo in your code: $GridField != $gridField
It should be:

$gridField = ...
$fields->addFieldToTab("Root.Chapters", $gridField); 

Avatar
oscarw_89

Community Member, 4 Posts

26 July 2012 at 7:19am

Haha

That was the problem.

And I spent hours looking for what was wrong..

Thanks for the help!