Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to merge two DataObjectSets

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

Page: 1
Go to End
Author Topic: 1581 Views
  • novaweb
    Avatar
    Community Member
    111 Posts

    How to merge two DataObjectSets Link to this post

    Hey guys,

    Really simple one here. I'm wondering if one can take two DataObjectSets (from two separate DataObject::get requests) and join them together.

    I have two different DataObjects, Module.php and Course.php. I want to call both of them using one function, so I can order both Modules AND Courses by Title ASC using a control block.

    Currently the code I have is:

    function Modules(){
    return DataObject::get("Module");
    }

    function Courses(){
    return DataObject::get("Course");
    }

    Can I do what i'm trying to achieve in it's own function? Or can I reference the above functions in another function to achieve the join?

    Cheers,
    Josh

  • dayer
    Avatar
    Community Member
    11 Posts

    Re: How to merge two DataObjectSets Link to this post

    I have the same doubt.

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: How to merge two DataObjectSets Link to this post

    how about...

    function Composite()
    {    
       $dosComposite = new DataObjectSet();
       $dosComposite->merge($this->Modules());      
       $dosComposite->merge($this->Courses());      
       $dosComposite->sort('FieldName','ASC');
       
       return $dosComposite;
    }

    1581 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.