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

Parsing raw sql output


Go to End


2 Posts   1294 Views

Avatar
paulm

Community Member, 1 Post

24 July 2015 at 2:16pm

Hello, I'm new to SilverStripe, so I'm still finding my way.
I have a page where I want to show a brief summary of all items added to the site in the last 60 days, ordered by date. The items are comming from 2 different database tables, so I need to use the SQL UNION function.
My SQL works fine, but looking through the documentation, it appears that UNION is not supported in SilverStripe.
First question: is there a way to do this without resorting to raw SQL?

I assume the answer is no, so I tried this:
$query = 'select "Title","Description","Created" from "ItemData" union select "Title","Description","Created" from "CollectionData" order by "Created"';
$latest = DB::query($query);
but the result appears to be incompatable with <% loop %> in my template.
Second question: how can I convert the result of the query to something I can itterate over in my template?

Third question:
Is there a better way to tackle this?

Thanks for any help on this, I imagine this is a reasonably common thing to want to do, so maybe I'm just missing somethig obvious.

Avatar
Nightjar

Community Member, 28 Posts

24 July 2015 at 10:31pm

Depends on your data source.
It looks like SS generated data though, so yes, it's possible. It usually involves having a superclass though. You'd execute a SuperClass::get() against it, which will include the appropriate subclass objects where applicable.
For the purpose of the query you're after though, a union is probably better (since it will span hierarchially unrelated classes by nature).
There is also http://api.silverstripe.org/3.1/class-DataList.html#_merge - but as it says there it's (apparently) not functional on DataLists, which is what you'd use for native object calls (Thing::get())

As for a better way to tackle it: You'd have to inspect SQLQuery class, but I don't think it supports UNION yet. In the next major update it may do, as the class has been split in four and you'd use SQLSelect instead. But I wouldn't particularly hold my breath - and there's no release window that I know of for this yet.

As for the results, you'd iterate through the result set, and instantiate model classes using the ClassName reference. You'd then insert them into an ArrayList to be output to the template. The way DataList handles this should provide good reference: http://api.silverstripe.org/3.1/source-class-DataList.html#669-694

Things in Silverstripe that extend ViewableData (so all Models, all Controllers) double as ViewModels. So to put info into templates you basically look for something that matches what you're after. To this end ArrayData is another class that could be of interest to you.