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.

Data Model Questions /

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

How to merge two DataObjectSets


Go to End


3 Posts   2949 Views

Avatar
novaweb

Community Member, 116 Posts

16 April 2010 at 9:25am

Edited: 16/04/2010 9:26am

Hey guys,

Really simple one here. I'm wondering if one can take two DataObjectSets (from two separate DataObject::get requests) and join them together.

I have two different DataObjects, Module.php and Course.php. I want to call both of them using one function, so I can order both Modules AND Courses by Title ASC using a control block.

Currently the code I have is:

function Modules(){
return DataObject::get("Module");
}

function Courses(){
return DataObject::get("Course");
}

Can I do what i'm trying to achieve in it's own function? Or can I reference the above functions in another function to achieve the join?

Cheers,
Josh

Avatar
dayer

Community Member, 11 Posts

17 August 2010 at 5:01am

I have the same doubt.

Avatar
swaiba

Forum Moderator, 1899 Posts

17 August 2010 at 9:01am

how about...


function Composite()
{ 	
	$dosComposite = new DataObjectSet();
	$dosComposite->merge($this->Modules());		
	$dosComposite->merge($this->Courses());		
	$dosComposite->sort('FieldName','ASC');
	
	return $dosComposite;
}