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

Multiple DataObject instances in Page


Go to End


1079 Views

Avatar
ROlckers

Community Member, 8 Posts

15 February 2012 at 9:04am

This topic should probably exist in the General category so please move this if it is not relevant to the DataObjectManager.

I have a DataObject class called MyDataModel. I also have a page called MyPage which has a 'has_many' relationship with MyDataModel. The MyDataModel has a 'has_one' relationship with MyPage.

I use the DOM inside MyPage to manage MyDataModel's data.

My question is this: could I have multiple instances of MyDataModel inside the MyPage class? MyModel1's tab's data should contain only the data as loaded through MyModel1's tab. The same goes for MyModel2's tab etc.. Hope this makes sense.

My gut tells me this is not possible by doing it the way I describe below as when I performed a dev/build with the additional 'instances' to MyDataModel, nothing happened to the table structure.

class MyPage extends Page {

	static $has_many = array (
		'MyDataModel1' => 'MyDataModel',
		'MyDataModel2' => 'MyDataModel',
		'MyDataModel3' => 'MyDataModel',
		'MyDataModel4' => 'MyDataModel'
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab("Root.Content.MyModel1", new DataObjectManager(
			$this,
			'MyDataModel1',
			'MyDataModel',
			array('Name'=>'Name'),
			'getCMSFields_forPopup'
		));
		
		$fields->addFieldToTab("Root.Content.MyModel2", new DataObjectManager(
			$this,
			'MyDataModel2',
			'MyDataModel',
			array('Name'=>'Name'),
			'getCMSFields_forPopup'
		));
		
		//... etc...
		
		return $fields;
	}

}