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

Linking DataObject to multiple page types


Go to End


2 Posts   1248 Views

Avatar
borriej

Community Member, 267 Posts

5 October 2011 at 11:53pm

Edited: 06/10/2011 12:11am

Hello,

pretty basic question, but i've only worked with has_one relations.

I have a dataobject linked to a page with a has_one relation like, but now i want to link the dataobject to two, or in the future three, different page types. The idea: I don't want a relation between the files under PageTypeA & PageTypeB. I just want to use the Video.php code again. So when i create a PageTypeB, i don't want to see the files in the manager that were created under PageTypeA.

The DataObject:


<?php
class Video extends DataObject
{
	static $db = array (
		'Title' => 'Varchar(100)',
		'Width' => 'Varchar(100)',
		'Height' => 'Varchar(100)',
		'Description' => 'Text'
	);
	
	static $has_one = array (
		// Make sure this comes first
		'PageTypeA' => 'PageTypeA',
		'File' => 'File'
	);
	
}

The PageTypeA:


<?php
class PageTypeA extends Page {
	static $db = array(
		);
	
	static $has_one = array(
	);
	
	static $has_many = array (
		'Videos' => 'Video'
	);

	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab("Root.Content.Videos", new FileDataObjectManager(
			$this,
			'Videos', // relation name
			'Video', // class name of the DataObject
			'File', // name of the file relation in the DataObject
			array('Title' => 'Title','Width' => 'Width','Height' => 'Height','Description' => 'Description'),
			new FieldSet(
				new TextField('Title'),
				new TextField('Width'),
				new TextField('Height', 'Height'),
				new TextareaField('Description')
			)
		));
		return $fields;
	}
}
class PageTypeA_Controller extends Page_Controller {
}

Now i want to link this DataObject as well to PageTypeB.
How do i do this?

Like this?
The DataObject:


<?php
class Video extends DataObject
{
	static $db = array (
		'Title' => 'Varchar(100)',
		'Width' => 'Varchar(100)',
		'Height' => 'Varchar(100)',
		'Description' => 'Text'
	);
	
	static $has_one = array (
		'File' => 'File'
	);
	static $has_many = array (
		'PageTypeA' => 'PageTypeA',
		'PageTypeB' => 'PageTypeA',
	);

	
}

Avatar
MarcusDalgren

Community Member, 288 Posts

6 October 2011 at 12:36am

To clear things up, do you want to link one video to several different pages or do you want to just link the relationship to several different page types but the video still only belongs to one page?

If you only want video to belong to one page at a time then continue using has_one. In your last code snippet it should read:

<?php 
class Video extends DataObject 
{ 
	static $db = array ( 
		'Title' => 'Varchar(100)', 
		'Width' => 'Varchar(100)', 
		'Height' => 'Varchar(100)', 
		'Description' => 'Text' 
	); 

	static $has_one = array ( 
		'PageTypeA' => 'PageTypeA', 
		'PageTypeB' => 'PageTypeB',
		'File' => 'File'
	); 
}

If however you want to link one video to several pages and vice versa you have to use $many_many instead. Another option is to let all the pages that have a video relation descend from the same parent. In that case all the sub classes get the same relationship as the parent.

For example

<?php 
class VideoPage extends Page 
{ 
	static $has_many = array ( 
		'Videos' => 'Video' 
	); 
	...
}
?>

Then you just sub class VideoPage for the pages that need to use the video.