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

SS3 DataObject merge - SOLVED


Go to End


6 Posts   7859 Views

Avatar
monkee

Community Member, 22 Posts

23 August 2012 at 3:26am

Edited: 23/08/2012 3:26am

I have two DataObject-Sets:

$set1 = DataObject::get('Visualisierung','Favorit = 1', 'RAND()');
$set2 = DataObject::get('Visualisierung','Favorit = 0', 'RAND()');

Now i want to merge these two to iterate them later in the template.
In SS 2.4 The Following worked:

$set1->merge($set2);

Now with SS3 i get the following error Message:
[User Error] Can't call DataList::merge() because its data comes from a specific query.

What is my error here?

Avatar
monkee

Community Member, 22 Posts

23 August 2012 at 3:34am

If i use
$newSet = DataObject::merge($set1, $set2);

I get an Error that the expected Object Class is wrong. (It expects the Page Class but i wanna merge pulled DataObjects)

Avatar
(deleted)

Community Member, 473 Posts

23 August 2012 at 6:18pm

With 3.0 and the lazy-loading ORM, you can't merge two DataLists. As your case is simple, you can simply use $set = Visualisierung::get()->filter('Favorit', array(1, 0))->sort('RAND()');

Avatar
monkee

Community Member, 22 Posts

23 August 2012 at 8:39pm

Unfortunately that does not produces the result i want. It just Randomizes ALL the Objects...

I try to achieve the following:
The First few Objects should be all "Favorite=1" in Random Order.
Followed by all Objects with "Favorite=0" in random Order

I think i will have to handle this in the template now if it is no more possible with the ORM... (Seems it has less features than before?)

Avatar
(deleted)

Community Member, 473 Posts

23 August 2012 at 8:43pm

In that case, you want Visualisierung::get()->filter('Favorit', array(1, 0))->sort('Favorit', 'desc')->sort('RAND()');

If you really need to run them separately then merge, you can use something like:

$set = new ArrayList;
foreach(Visualisierung::get()->filter('Favorit', 1)->sort('RAND()') as $obj) $set->push($obj);
foreach(Visualisierung::get()->filter('Favorit', 0)->sort('RAND()') as $obj) $set->push($obj);
return $set;

which is pretty much what was happening in 2.4.

Avatar
monkee

Community Member, 22 Posts

23 August 2012 at 9:21pm

Edited: 23/08/2012 9:37pm

I really needed to run them seperately.

object-> push works perfect!

On http://klarbild.probiermau.ch/ you can see how i use it...
First are all "Favorite=1" Objects(Picture shown) in Random Order.
The last two Rows are all "Favorite=0" Visualisierung Objects in Random Order.

Simon_w you are officially my hero for today!

Thanks a lot for your kind help!