3063 Posts in 864 Topics by 646 members
| Go to End | Next > | |
| Author | Topic: | 3174 Views |
-
Combining two DataObject

25 March 2009 at 10:13pm Last edited: 25 March 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
-
Re: Combining two DataObject

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 -
Re: Combining two DataObject

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
-
Re: Combining two DataObject

27 March 2009 at 8:39am Last edited: 27 March 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")); -
Re: Combining two DataObject

9 September 2010 at 4:22pm Last edited: 9 September 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
-
Re: Combining two DataObject

30 September 2010 at 12:36am
I'd recommend using ->removeDuplicates after the "merge" as "merge" really means "append" using this function.
-
Re: Combining two DataObject

17 March 2011 at 6:35am
Okay, i have to merge two MemberObjects into one
Breeder extends Member
Owner extends MemberDog 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
)
| 3174 Views | ||
| Go to Top | Next > |





