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

SQLQuery() Question


Reply

6 Posts   1684 Views

Avatar
Garrett

13 October 2009 at 9:20am Community Member, 245 Posts

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

Avatar
dalesaurus

13 October 2009 at 1:18pm Community Member, 283 Posts

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

Avatar
Garrett

13 October 2009 at 2:02pm Community Member, 245 Posts

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

Avatar
Hamish

14 October 2009 at 1:33pm Community Member, 712 Posts

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.

Avatar
dalesaurus

16 October 2009 at 6:04pm Community Member, 283 Posts

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?

Avatar
Garrett

17 October 2009 at 10:22am Community Member, 245 Posts

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.