Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SQLQuery() Question

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

Page: 1
Go to End
Author Topic: 1674 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    SQLQuery() Question Link to this post

    Can I use a Union in this method?

    $query->select = array("blah blah blah");
    $query->from = array("blah blah blah");
    $query->where = array("blah blah blah");
    $query->groupby = array("blah blah blah");

    $query->union = array("blah blah blah"); ???

    $query->orderby = "blah blah blah";
    $query->limit = "blah blah blah";

    In the same query I ned to search BOTH the SiteTree table AND some DataObjectManager tables, which is why I need the Union(s). Any ideas?

    Thanks,
    Garrett

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: SQLQuery() Question Link to this post

    How about a LEFT JOIN instead, or a HAVING? Those are in there.

    Last resort you could just generate the raw SQL and fire it of with DB::query();

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: SQLQuery() Question Link to this post

    Hi, thanks for your reply. I would use DB::query() if I didn't need Pagination. ParseQueryLimit() doesn't work with DB::query(), so I think I Have to use SQLQuery(). And a JOIN won't work because these DataObjectManager-created tables have No relationship to SiteTree. And HAVING, well, I don't know how that would work. Further ideas?

    Thanks again,
    Garrett

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: SQLQuery() Question Link to this post

    There are a few issues here. Firstly, if you have disparate objects, you have the potential for ID collisions which SilverStripe is not going to like in certain situations. Secondly, it isn't even clear how you paginate over a unioned dataset. Consider the differences between:

    (SELECT ID FROM MyObject LIMIT 5) UNION (SELECT ID FROM MyOtherUnrelatedObject LIMIT 5)

    (SELECT ID FROM MyObject UNION SELECT ID FROM MyOtherUnrelatedObject) LIMIT 5

    Basically, to keep it stable you'll have to write the sql and handle the pagination yourself. You could also try merging two dataobject sets, eg:

    $ds1 = DataObject::get('MyObject');
    $ds2 = DataObject::get('MyOtherUnrelatedObject');
    $ds1->merge($ds2);

    But it doesn't really make pagination any easier.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: SQLQuery() Question Link to this post

    I can't see how any type of relationship created by the ORM would not be joinable with SQLQuery and formatting into a DataSetObject. Have you tried getting into your data via a real query browser?

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: SQLQuery() Question Link to this post

    Yes. And I am using DB::query() now because of this Union issue. I just have to go with no paging, I guess, or try the merge() as suggested by @hamish.

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