Disclaimer, I know next to nothing about Yii….
Today I had an issue with a Yii App and it’s default admin CRUD tables, and sorting fields, well everything that listed a record from the database was in the order it was inserted into the database and not in a human friendly alphabetical order.
It turns out it’s really easy to set a default so that everything that uses the Model, comes out in a sorted way. (Note for larger things, you probably don’t want every single query that hits the DB getting sorted, so you’d probably only apply the sorting as required, but for small things, this shouldn’t be an issue).
For this example I have a Model called ‘Contacts’.
Open up your model (mine is: /models/Contacts.php)
Above the function ‘ public static function model($className=__CLASS__)’
Add the following function (if it’s not present)
* Let's Setup some defaults!
public function defaultScope()
'order' => 'FirstName'
In the array of ‘order’ change the value to the name of the column that you want to sort by, for my example it was ‘FirstName’.
Save, upload, test, enjoy.
Like I said at the start, I know nothing about Yii, had I, I probably wouldn’t have spent 20 minutes googling it.