Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Combining two DataObject

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

Page: 1 2
Go to End
Author Topic: 4256 Views
  • Victor
    Avatar
    Community Member
    128 Posts

    Combining two DataObject Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Combining two DataObject Link to this post

    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

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Combining two DataObject Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Combining two DataObject Link to this post

    "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"));

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Combining two DataObject Link to this post

    Thanks!

  • kuenkuen82
    Avatar
    Community Member
    41 Posts

    Re: Combining two DataObject Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Combining two DataObject Link to this post

    I'd recommend using ->removeDuplicates after the "merge" as "merge" really means "append" using this function.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Combining two DataObject Link to this post

    Okay, i have to merge two MemberObjects into one

    Breeder extends Member
    Owner extends Member

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

    4256 Views
Page: 1 2
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.