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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DOM for Pages - how to set ParentID upon write?


Go to End


2 Posts   1019 Views

Avatar
DesignCollective

Community Member, 66 Posts

10 June 2011 at 10:06am

Edited: 10/06/2011 10:10am

I am setting up a DOM for Articles under a Category page. When added via the DOM, I want the Articles to become children of the Category, and I will have them hidden from SiteTree. As they are added via the DOM, to set the ParentID to the ID of the Category and I need help with this.

I tried this... but it's not working.


class Article extends Page {

 static $db = array(
 	'Subtitle' => 'Varchar(60)',
 	'Summary' => 'Text',
 	'Author'=>'Text'
 );
	static $has_one = array(
		'Category' => 'Category'
	);
	
	static $allowed_children = array('None');
	
	
	static $defaults = array(
		'ShowInMenus' => false,
		'ShowInSearch' => false
	);
	
	function onBeforeWrite() {
		if(!$this->ID) {
			$this->ParentID = $this->Category()->ID;
		}
		parent::onBeforeWrite();
	}
}

Should I use setParent()?

I'm also wondering - the CategoryID (from $has_one) and ParentID will eventually be the same (once we figure it out), not sure if both are needed or if there's a way for DOM to seek ParentID and not need the $has_one reference.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

11 June 2011 at 3:29am

DOM is for managing DataObject subclasses -- not SiteTree. If you're looking for an alternative to the tree for managing your pages, you might check out my tutorial "Remodel Admin" on my blog.

http://www.leftandmain.com/uncategorized/2011/02/25/taming-the-beast-remodeling-modeladmin/

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