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.

Data Model Questions

SS3 DataObject merge - SOLVED


6 Posts   2354 Views


23 August 2012 at 3:26am (Last edited: 23 August 2012 3:26am), Community Member, 20 Posts

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:


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?


23 August 2012 at 3:34am Community Member, 20 Posts

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)


23 August 2012 at 6:18pm Forum Moderator, 474 Posts

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()');


23 August 2012 at 8:39pm Community Member, 20 Posts

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?)


23 August 2012 at 8:43pm Forum Moderator, 474 Posts

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.


23 August 2012 at 9:21pm (Last edited: 23 August 2012 9:37pm), Community Member, 20 Posts

I really needed to run them seperately.

object-> push works perfect!

On 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!