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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Solved: Decorator Classes with DataObjectManager


Go to End


2 Posts   992 Views

Avatar
Sewer

Community Member, 9 Posts

29 November 2011 at 10:56pm

First of all, I'd like to say hello to all forum members.
I am a graphic designer and I'm using SilverStripe for small company websites mostly.
I found out that there are many alements repeating in each project, so I'd like to standarize them.
So I decided to use DataObjectDecorators for things like animated slideshows, link groups on Home Page etc, which will be added or not.

I don't know what am I doing wrong, but there are some problems with has_many relation.
I have tho decorators - one is working, the other is not working.
I'm pasting code and dev error.

SlidesHolder with Slider - this is working

<?php

class SlidesHolder extends DataObjectDecorator {

	public function extraStatics(){
		return array(
			'has_many' => array(
				'Slides' => 'Slide'
			)
		);
	}

	public function updateCMSFields(FieldSet &$fields) {
		
		$t_slides = _t('Backend.SLIDES', 'Slideshow');
		
		$fields->addFieldToTab("Root.Content.$t_slides", new HeaderField('Title', 'Title'));
		
		$fields->addFieldToTab("Root.Content.$t_slides", new ImageDataObjectManager(
			$this->owner,
			'Slides',
			'Slide',
			'Photo',
			array(
				'Name'=>'Name'	
			),
			'getCMSFields_forPopup',
			NULL,
			NULL
		));
	}

}

<?php
class Slide extends DataObject {
	
	static $db = array (
	'Name' => 'Varchar(255)',
	'Desc' => 'Text',
	'SortOrder' => 'Int'
	);
	
	static $has_one = array (
	'Photo' => 'Image',
	'PageLink' => 'SiteTree',
	'SlidesHolder' => 'SlidesHolder'
	);
	
	static $default_sort = "SortOrder ASC";  
	
	public function getCMSFields_forPopup() {
		
		return new FieldSet (		
			new TextField('Name', 'Nazwa'),
			new ImageField('Photo', 'Zdjęcie'),
			new TextareaField('Desc', 'Opis'),
			new SimpleTreeDropdownField('PageLinkID', "Link do strony", 'SiteTree'),
			new NumericField ('SortOrder', 'Kolejność', '', 3)			
		);
	}		
	
}


Object::add_extension(SliderConfig::$PageToApply, 'SlidesHolder'); - in _config.php

GraphicLinksHolder with Graphic link - this is not working properly. I can add items by DataObjectManager, but there is error when I try to edit them.
Here's the code:

<?php

class GraphicLinksHolder extends DataObjectDecorator {

	public function extraStatics(){
		return array(
			'db' => array(
			),
			
			'has_many' => array (
				'GraphicLinks' => 'GraphicLink'
			)
		);
	}
    
	public function updateCMSFields(FieldSet &$fields) {
		
		$t_graphiclinks = _t('Backend.GRAPHICLINKS', 'Graphic links');
		$t_links = _t('Backend.LINKS', 'Links');
		
		$fields->addFieldToTab("Root.Content.$t_links", new HeaderField('Title', "$t_graphiclinks"));
		$fields->addFieldToTab("Root.Content.$t_links", new DataObjectManager(
			$this->owner,
			'GraphicLinks',
			'GraphicLink',
			array(
				'Name' => 'Name'
			),
			'getCMSFields_forPopup',
			NULL,
			NULL
		));
	
	}

}


<?php

class GraphicLink extends DataObject
{
	static $db = array (
		'Name' => 'Text',
	);
 
	static $has_one = array (
		'GraphicLinksHolder' => 'GraphicLinksHolder',
		'Photo' => 'Image'
	);
 
	public function getCMSFields_forPopup()
	{
		return new FieldSet(
			new TextField('Name'),
			new ImageField('Photo')
		);
	}
}


Object::add_extension('HomePage', 'GraphicLinksHolder'); - in _Config.php

Error:

[User Error] Couldn't run query: SELECT "GraphicLink"."ClassName", "GraphicLink"."Created", "GraphicLink"."LastEdited", "GraphicLink"."Name", "GraphicLink"."GraphicLinksHolderID", "GraphicLink"."PhotoID", "GraphicLink"."ID", CASE WHEN "GraphicLink"."ClassName" IS NOT NULL THEN "GraphicLink"."ClassName" ELSE 'GraphicLink' END AS "RecordClassName" FROM "GraphicLink" WHERE ("ParentID" = '1') Unknown column 'ParentID' in 'where clause'

Avatar
Sewer

Community Member, 9 Posts

29 November 2011 at 11:21pm

I've found it out.
Has_one relation in DataObject needs to point on decorated class, not to decorator itself.