[SOLVED] listData sorting
#1
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'.
Greetings
Dan
#3
Posted 23 March 2009 - 10:32 AM
Quote
unfortunately this returns an error:
"Invalid argument supplied for foreach()"
#4
Posted 23 March 2009 - 10:44 AM
#5
Posted 23 March 2009 - 10:53 AM
Quote
which means?
no sorting for this function?
#6
Posted 23 March 2009 - 10:58 AM
#7
Posted 23 March 2009 - 11:05 AM
#8
Posted 23 March 2009 - 11:17 AM
#9
Posted 23 March 2009 - 11:18 AM
CHtml::activeDropDownList($model, 'idModel', CHtml::listData(MyModel::model()->findAll(array('order'=>'strName ASC')), 'id', 'strName'));
If it doesn't work like this, look in the documentation in the database section.
#11
Posted 23 March 2009 - 11:21 AM
I agree in doing this on the database.
Perfect. Thanks a lot guys.
#12
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!
#13
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.
#14
Posted 14 December 2011 - 06:38 PM
c@cba, on 14 December 2011 - 06:31 PM, said:
It's no wonder.. You're posting in the Yii 1.0 forum. Try the 1.1 forum.

Help

This topic is locked











