Yii Framework Forum: [SOLVED] listData sorting - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

[SOLVED] listData sorting Rate Topic: -----

#1 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 23 March 2009 - 10:03 AM

Can anybody tell me how I can add sorting to the following code?

CHtml::activeDropDownList($model, 'idModel', CHtml::listData(MyModel::model()->findAll(), 'id', 'strName'));

I'd like to get listData ordered by the column 'strName'.

Greetings
Dan
0

#2 User is online   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 March 2009 - 10:13 AM

asort(CHtml::listData(...))
0

#3 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 23 March 2009 - 10:32 AM

Quote

asort(CHtml::listData(...))


unfortunately this returns an error:

"Invalid argument supplied for foreach()"
0

#4 User is online   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 March 2009 - 10:44 AM

sorry, asort() expects the first parameter to be variable because it is using reference.
0

#5 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 23 March 2009 - 10:53 AM

Quote

sorry, asort() expects the first parameter to be variable because it is using reference.


which means?
no sorting for this function?
0

#6 User is offline   dalip 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 21-December 08

Posted 23 March 2009 - 10:58 AM

Why don't you sort on retrieval from your model. Your database probably is faster at sorting stuff than php is. Just set the 'order' parameter to 'strName ASC'
0

#7 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 23 March 2009 - 11:05 AM

ok, but in which function should I do this (I'm a newbie in Yii :-P)?
0

#8 User is online   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 March 2009 - 11:17 AM

You could write a method in MyModel and call this method in view to get the sorted list data.
0

#9 User is offline   dalip 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 21-December 08

Posted 23 March 2009 - 11:18 AM

IIRC

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.
1

#10 User is online   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 March 2009 - 11:19 AM

Yeah, this is better by using DB sorting.
0

#11 User is offline   bitmatix 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 23-March 09

Posted 23 March 2009 - 11:21 AM

Yeah, works perfectly :-)

I agree in doing this on the database.

Perfect. Thanks a lot guys.
0

#12 User is offline   SteveWa 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 14-January 11

Posted 18 January 2011 - 03:46 PM

I would have never thought that is where the sort by clause would go. I misunderstood the findAll argument "condition" to just be the criteria of the SQL statement's WHERE clause, and as a result, I thought there must be a different way to specify the order by clause.

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!
0

#13 User is offline   c@cba 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 02-December 11

Posted 14 December 2011 - 06:31 PM

In my case the following DIDN'T work:
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.
0

#14 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 14 December 2011 - 06:38 PM

View Postc@cba, on 14 December 2011 - 06:31 PM, said:

In my case the following DIDN'T work:

It's no wonder.. You're posting in the Yii 1.0 forum. Try the 1.1 forum. :)
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users