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

Problems using DOM on has_many on a related DataObject


Go to End


3 Posts   939 Views

Avatar
gieser

Community Member, 13 Posts

7 August 2011 at 11:57pm

Hi guys,
I'm currently trying to use the DataObjectManager in the following way:

(1) I have a class NewsItem:

class NewsItem extends DataObject {
	static $db = array(...);
	static $has_one = array("NewsStream" => "NewsStream", ...);
}

(2) A class NewsStream:

class NewsStream extends DataObject {
	static $db = array(...);
	static $has_many = array(..., "NewsItems" => "NewsItem");
}

(3) And I have a Decorator to add NewsStreams to pages:

class NewsStreamDecorator extends DataObjectDecorator {
	
	function extraStatics() {
		return array("has_one" => array("NewsStrom" => "NewsStream"));
	}
	
	public function updateCMSFields(FieldSet $fields) {
		if(($streams = DataObject::get("NewsStream")) != null) 
			$fields->addFieldToTab("Root.Content.News", new DropdownField("NewsStromID", "Neuigkeiten Strom", $streams->toDropdownMap("ID", "Title")));
		else 
			$fields->addFieldToTab("Root.Content.News", new LiteralField("Message", "Keine Newsströme im System vorhanden!"));
		
		if(($stream = $this->owner->NewsStrom()) != null ) {
			$dom = new DataObjectManager($this->owner, "NewsStrom->NewsItems", "NewsItem", array("Title" => "Titel", "ForceHomepage" => "Auf Homepage anzeigen", "ShowGallery" => "Hat eine Galerie"), "getCMSFields_forPopup", "NewsStreamID = ".$stream->ID);
			$fields->addFieldToTab("Root.Content.News", $dom);
		}
		return $fields;
	}
	
}

If I know try to add NewsItems via the DOM they will get written without a NewsStreamID. Any idea how to solve this problem?

thanks in advance,
gieser

Avatar
UncleCheese

Forum Moderator, 4102 Posts

10 August 2011 at 5:21am

I'm not sure what the decorator is for?

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Avatar
gieser

Community Member, 13 Posts

10 August 2011 at 5:25am

ah sorry, didn't mention that. It is used to add NewsStream to those page types that need it. Didn't want to repeat myself you know.