Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 DataObject merge - SOLVED

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

Page: 1
Go to End
Author Topic: 1812 Views
  • monkee
    Avatar
    Community Member
    20 Posts

    SS3 DataObject merge - SOLVED Link to this post

    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?

  • monkee
    Avatar
    Community Member
    20 Posts

    Re: SS3 DataObject merge - SOLVED Link to this post

    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)

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: SS3 DataObject merge - SOLVED Link to this post

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

  • monkee
    Avatar
    Community Member
    20 Posts

    Re: SS3 DataObject merge - SOLVED Link to this post

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

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: SS3 DataObject merge - SOLVED Link to this post

    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.

  • monkee
    Avatar
    Community Member
    20 Posts

    Re: SS3 DataObject merge - SOLVED Link to this post

    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!

    1812 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.