[SOLVED] listData sorting
Posted 23 March 2009 - 10:03 AM
CHtml::activeDropDownList($model, 'idModel', CHtml::listData(MyModel::model()->findAll(), 'id', 'strName'));
I'd like to get listData ordered by the column 'strName'.
Posted 18 January 2011 - 03:46 PM
Now I see from the api doc, the condition type can be mixed, either a string or an array.
So it also works as findAll('1=1 order by name'),
I did not understand the reason for the array, since it was being passed only one value, until I viewed the API section for CDbCriteria and the __construct() method, which shows that it expects an array as input, and does a foreach loop for each key value pair of the input array, and then assigns properties to the CDbCriteria object... it's nice to have good documentation, thank you!
Posted 14 December 2011 - 06:31 PM
asort(CHtml::listData(Country::model()->findAll(array('order'=>'t.Name')), 'ID', 'Name'));
But the following DID work:
$data = CHtml::listData(Country::model()->findAll(array('order'=>'t.Name')), 'ID', 'Name'); asort($data);
Just wanted to mention it, in case you need to sort after results are returned.
For example: I have the 'order' clause in my SQL, but in some cases I manipulate the data in the 'afterFind()' method, so that the eventual list is not ordered correctly anymore.
PS: 'afterFind()' is the method executed when the onAfterFind() event of CActiveRecord is raised.