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:

Data Model Questions /

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

Combining two DataObject

Go to End

10 Posts   6020 Views


Community Member, 128 Posts

25 March 2009 at 10:13pm

Edited: 25/03/2009 10:13pm

I have two statements of the type

$results=DataObject::get (" ", " ");

they contain exactly the same fields (but from different tables and may be with different SELECT criteria). How can I combine them (to implement UNION ALL)?

Thank you in advance. Victor

Ben Gribaudo

Community Member, 181 Posts

27 March 2009 at 2:50am

Hi Victor,

If you don't mind loading each set using a separate statement, then merging them in memory in SilverStripe, you could try DataObjectSet's merge method. (Remember that DataObject::get returns the resulting data set in a DataObjectSet.)

I think this would end up looking something like:

$results1=DataObject::get (" ", " ");
$results2.merge(DataObject::get (" ", " "));

Hope this helps,


Community Member, 128 Posts

27 March 2009 at 4:21am

It does not work, but huge thanks for pointing in the right direction:

$results= DataObject::get("blah");
$results2= DataObject::get("crap");

Now according to


and it works unless $results is empty in which case it bombs to

Fatal error: Call to a member function merge() on a non-object in ... on line ...

Then following the same tutorial I try


but it still bombs

Fatal error: Call to a member function exists() on a non-object in ... on line ...

Any suggestion? Thanks. Victor


Community Member, 712 Posts

27 March 2009 at 8:39am

Edited: 27/03/2009 8:40am

"exists" is a bit of a misnomer. DataObject will return nothing (false) if the query is empty. That always bugs me a little - breaks some of the chainability. A better way is to create an empty set first, then append to it. Then it doesn't matter if the results exist or not:

$ds = new DataObjectSet();


Community Member, 128 Posts

27 March 2009 at 9:57am



Community Member, 41 Posts

9 September 2010 at 4:22pm

Edited: 09/09/2010 8:21pm

This is great, would I be able to sort the data? for example order by date?

It's so simple just use:

$ds->sort('Date', 'DESC');// 2nd param not required ASC is default


Forum Moderator, 1899 Posts

30 September 2010 at 12:36am

I'd recommend using ->removeDuplicates after the "merge" as "merge" really means "append" using this function.


Community Member, 110 Posts

17 March 2011 at 6:35am

Okay, i have to merge two MemberObjects into one

Breeder extends Member
Owner extends Member

Dog has one Breeder and one Owner

The Owner isn't always a Breeder ( but can be)
The Breeder doesn't have to be the Owner (but can be)

At my DOG i have to;

- choose the Breeder (that's easy )

- choose the owner; 1) The Owner is not a Breeder (then it's easy, since the owners are members also)
2) The Owner is the same as the breeder ( well got that..leave owner empty and if-else)
3) The Owner is a Breeder, but not of this Dog.

So in my Dog object i have to make a 'Owner' DropDownField that shows the Breeders and the Owners in one list. Then my Dog has to know if his owner comes from the Owner DO or from the Breeder DO

I'm so lost (again,but ....learning :) )

Go to Top