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

Makings items unique per page: DataObjectManager


Go to End


6 Posts   1567 Views

Avatar
Deklin

Community Member, 16 Posts

14 July 2010 at 3:20am

I want each page to contain a list of links, specific to that page. The problem with my code is that is displays the same links on every page.

Each set of links should only be displayed on the page that it was created for.

Page.php

      $fields->addFieldToTab("Root.Content.Links", new DataObjectManager(
			$this,
			'FooterMenuItem',
			'FooterMenuItem',
			array('Link'=>'Link','Text'=>'Text'),
			'getCMSFields_forPopup'
	));

FooterMenuItem.php

class FooterMenuItem extends DataObject
{
	static $db = array (
		'Link' => 'Varchar',
		'Text' => 'Varchar'
	);
 
	public function getCMSFields_forPopup()
	{
		return new FieldSet(
			new TextField('Link'),
			new TextField('Text')
		);
	}
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 July 2010 at 4:20am

If you're going to be using the DataObjectManager on subclasses of the object where it is defined, you need to use setParentClass($parent) (in your case, "Page")..

Avatar
Deklin

Community Member, 16 Posts

14 July 2010 at 5:33am

Thanks, where does the setParentClass code go? Do I need to need to change this to has_one or has_many also?

//Page.php

$manager = new DataObjectManager(
			$this,
			'FooterMenuItem',
			'FooterMenuItem',
			array('Link'=>'Link','Text'=>'Text'),
			'getCMSFields_forPopup'
	)

$manager->setParentClass('Page');

      $fields->addFieldToTab("Root.Content.Links", $manager);

//FooterMenuItem.php
class FooterMenuItem extends DataObject
{
	static $db = array (
		'Link' => 'Varchar',
		'Text' => 'Varchar'
	);
 
	public function getCMSFields_forPopup()
	{
		return new FieldSet(
			new TextField('Link'),
			new TextField('Text')
		);
	}
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 July 2010 at 5:49am

Make sure Page has $has_many FooterMenuItem and FooterMenuItem $has_one Page.

Avatar
Deklin

Community Member, 16 Posts

14 July 2010 at 6:35am

Thanks but I am still at a loss. How do I make sure Page has $has_many FooterMenuItem and FooterMenuItem $has_one Page?

My code is posted in my previous link.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 July 2010 at 7:47am

Have you read the DOM documentation? You need to set up a relationship between Page and FooterLinks.

Page:

static has_many = array (
'FooterLinks' => 'FooterLink'
);

FooterLink:

static $has_one = array (
'Page' => 'Page'
);