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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

SQLQuery() Question


Go to End


7 Posts   3206 Views

Avatar
Garrett

Community Member, 245 Posts

13 October 2009 at 9:20am

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

Community Member, 283 Posts

13 October 2009 at 1:18pm

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

Community Member, 245 Posts

13 October 2009 at 2:02pm

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

Community Member, 712 Posts

14 October 2009 at 1:33pm

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

Community Member, 283 Posts

16 October 2009 at 6:04pm

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

Community Member, 245 Posts

17 October 2009 at 10:22am

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.

Avatar
mlevens

Community Member, 5 Posts

15 October 2014 at 1:17pm

You can push the results returned from DB::query($sql) onto a PaginatedList

Example:

...
$result = DB::query($sql);
$returnedRecords = new ArrayList();

foreach($result as $row) 
{
  $returnedRecords->push(new ArrayData($row));
}
$returnedRecords = new PaginatedList($returnedRecords);
...