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

How do i use groupby filter on datalist


Go to End


3 Posts   1673 Views

Avatar
Optic Blaze

Community Member, 190 Posts

13 August 2016 at 11:04pm

Hi there,

I have a set of data like this in a databoject called MyFruit
Name FruitPreference
Bob apple
Steve grape
Joe apple
James bannana
Sandra grape
Peter apple

I want to be able to query the dataobject and get a list of fruit from it. The kind of thing you would do with a SQL query using the groupby function. I know how to do it using the 'new SQLSelect' method, but i want to know how to use it with the easier syntax that we use in silverstripe....something like

$myresults = MyFruit::get()->distinct('FruitPreference')->sort('FruitPreference', 'ASC');

Avatar
Jare

Community Member, 39 Posts

16 August 2016 at 10:39am

I'm really not sure about this at all, but have you tried:

$myresults = MyFruit::get()->distinct(true)->column('FruitPreference');

The column() method returns a simple array containing values from only the specified column, but I don't know whether the preceding distinct() method call will affect it or not. If it works, you should be able to avoid duplicate results, but I can't guarantee as I haven't tested the code.

Avatar
Optic Blaze

Community Member, 190 Posts

16 August 2016 at 7:37pm

Hi there,

Thanks for this...it worked!!!