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

Getting related Objects [RESOLVED]


Go to End


3 Posts   1547 Views

Avatar
VictorH

Community Member, 29 Posts

11 August 2011 at 7:29am

I can't figure out how to display the "Resources Link" under there appropriate "Resource Link Category" any help?

Here's the code.

ResourceLinkCategory.php

class ResourceLinkCategory extends DataObject {
	static $db = array('Name' => 'Text');
	
	static $has_many = array('ResourceLinks' => 'ResourceLink');

	function getCMSFields_forPopup() {
		$fields = new FieldSet();

		$fields->push (new TextField('Name'));

		return $fields;
	}
}

ResourceLink.php

class ResourceLink extends DataObject {
	static $db = array(
		'URL' => 'Text',
		'Description' => 'Text'
	);
	
	static $has_one = array(
		'ResourceLinkPage' => 'ResourceLinkPage',
		'MyResourceLinkCategory' => 'ResourceLinkCategory'
	);

	function getCMSFields_forPopup() {
		$fields = new FieldSet();

		$fields->push (new TextField('URL'));
		$fields->push (new TextareaField('Description'));
		$fields->push (new HasOneDataObjectManager(
		   $this,
		   'MyResourceLinkCategory',
		   'ResourceLinkCategory',
		   array('Name' => 'Name'),
		   'getCMSFields'
		));

		return $fields;
	}
}

ResourceLinkPage.php

class ResourceLinkPage extends Page {
	static $has_many = array(
		'ResourceLinks' => 'ResourceLink',
		'ResourceLinkCategories' => 'ResourceLinkCategory'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$resourcelinks = new DataObjectManager(
			$this,
			'ResourceLinks',
			'ResourceLink',
			array(
				'URL' => 'URL',
				'Description' => 'Description'
			),
			'getCMSFields_forPopup'
		);
		$fields->addFieldToTab('Root.Content.ResourceLinks', $resourcelinks);
		
		return $fields;
	}
}

class ResourceLinkPage_Controller extends Page_Controller {
	function GetResourceLinkCategories() {
		return DataObject::get('ResourceLinkCategory', '', '', '', '');
	}
}

ResourceLinkPage.ss

<% control GetResourceLinkCategories %>
	<h2><span>$Name</span></h2>
        <-- Display Resources Link related to the Category here -->
<% end_control %>

Avatar
MarcusDalgren

Community Member, 288 Posts

11 August 2011 at 9:38am

<% control GetResourceLinkCategories %> 
	<h2><span>$Name</span></h2> 
	<% control ResourceLinks %>
	<!-- Resource link stuff here -->	
	<% end_control %>
<% end_control %>

There's always a method available with the same name as the relationship which acts as a getter for all related objects.

Avatar
VictorH

Community Member, 29 Posts

11 August 2011 at 10:00am

*sigh* I need some sleep. Thanks that works just like it should.