Yii Framework Forum: ORDER BY in CActiveRecord::findAll() - Yii Framework Forum

Jump to content

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

ORDER BY in CActiveRecord::findAll() Rate Topic: ***** 3 Votes

#1 User is offline   brooss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 16-November 08

Posted 25 November 2008 - 08:56 PM

The following seems to work:
SomeActiveRecord::Model()->findAll("ORDER BY somefieldfield");

But is it correct to put an ORDER BY clause in a findAll like this or is there a better way to do a simple order by (without going to the trouble of creating a full criteria)

The Class Reference seems to hint at another way by passing an array to findAll() but I couldn't get it to work. An example would be helpful.
0

#2 User is offline   qiang 

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

Posted 25 November 2008 - 09:02 PM

*
POPULAR

This won't work because the generated SQL will be something like
SELECT * FROM table WHERE ORDER BY something

You can use the following if you don't want to create a db criteria object:

SomeActiveRecord::model()->findAll(array('order'=>'somefieldfield'));


7

#3 User is offline   brooss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 16-November 08

Posted 25 November 2008 - 09:27 PM

*
POPULAR

I cut my example down too much I guess, so it works with
findAll("otherfield=:x ORDER BY somefield", array(':x'=>$x));

So is it correct to do it this way or should I pass in the criteria array?

For this example, if I understand correctly, it would be:
findAll(array('order'=>'somefield', 'condition'=>'otherfield=:x', 'params'=>array(':x'=>$x)));

5

#4 User is offline   qiang 

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

Posted 25 November 2008 - 09:38 PM

*
POPULAR

Your first approach is kind of hacking because the SQL will be generated simply by appending the first string parameter. So it works.

The proper way is your second approach.
6

#5 User is offline   brooss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 16-November 08

Posted 25 November 2008 - 10:24 PM

Thanks, that's what I thought.

I would suggest an example of using the second method be added to the The Definitive Guide - ActiveRecord page. There's no mention at all of ordering results on the page and only one example of creating a criteria object manually and passing that.
1

#6 User is offline   qiang 

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

Posted 25 November 2008 - 10:40 PM

Changed the guide per your request. Thanks.
1

#7 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 879
  • Joined: 20-April 11
  • Location:India

Posted 20 September 2011 - 02:21 AM

Thanks For the Information.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

Share this topic:


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

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