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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Sort problem in ModelAdmin´s listview


Go to End


4 Posts   1161 Views

Avatar
Bereusei

Community Member, 96 Posts

5 May 2015 at 7:36pm

Hey guys,

I want to sort the results of the list in ModelAdmin.

public function getList(){
        $list = parent::getList();

        $array = array("983,96,227,547,588,38,580,984,142,614,144,417,851,985,28,179,866,867,868,865");
        $list = $list->where("ID IN (".implode(',',$array).")");
       
        return $list;
    }

Generally this kind of code is useful: $list = $list->where("ID IN (".implode(',',$array).")")->sort(array("Title" => "ASC")
But I want that the results are listed in order from the array, so in my example ID 983,96,227,547...
But the results started somewhere in the middle of the array (with ID 179).

Anyone an idea how to fix this?

Avatar
Pyromanik

Community Member, 419 Posts

5 May 2015 at 9:18pm

I can come across as a complete prick sometimes, so please excuse me in advance. I don't mean to be, I'm just quick to the point.

1. Sorting ModelAdmin is generally a terrible idea.
2. Using $list->where() is generally doing it wrong.

ModelAdmin lists all things, and is sortable by default, and more importantly, filterable.
With filters in use, the sort changes (because things are missing) in which case clients then go and try to sort things, only to have unexpected results because records are missing in the middle causing all kinds of havoc. A better idea is probably to subclass ModelAdmin and remove all filtering (ie, customise the purpose to fit the usecase).

The whole point of an ORM is to abstract the query and avoid SQL. where() should only be used in rare circumstances where the logic is too complex to be achieved with a filter.

simply: $list->filter('ID', $array)
Or if you have multiple filters it takes a keyed array: $list->filter(['ID'=>$array, ...])

Avatar
Bereusei

Community Member, 96 Posts

5 May 2015 at 10:06pm

Thanks, for the fast feedback. I´ve replaced the 'where()-function' in my code by the 'filter()-functon'. Now the my code is a little bit cleaner.
Your clue about ModelAdmin is plausible. I keep that in mind.
Thanks for your help.

Avatar
swaiba

Forum Moderator, 1899 Posts

6 May 2015 at 1:14am

An alternative is to set a default sort on the DataObject itself http://api.silverstripe.org/3.1/source-class-DataObject.html#$default_sort - e.g.

static $default_sort = 'Name ASC';

So that in every case this object is retrieved it will be in the correct order.