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

Combining two DataObject


Go to End


10 Posts   7092 Views

Avatar
Victor

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

Avatar
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,
Ben

Avatar
Victor

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
http://doc.silverstripe.org/doku.php?id=dataobjectset&s=merge#merge_with_other_dataobjectsets

$results->merge($results2);

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

if($results->exists())
...
else
$results=$results2;

but it still bombs

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

Any suggestion? Thanks. Victor

Avatar
Hamish

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();
$ds->merge(DataObject::get("Object1"));
$ds->merge(DataObject::get("Object2"));

Avatar
Victor

Community Member, 128 Posts

27 March 2009 at 9:57am

Thanks!

Avatar
kuenkuen82

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

Avatar
swaiba

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.

Avatar
Stefdv

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