ORDER BY in CActiveRecord::findAll()

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.

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:


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)));

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.

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.

Changed the guide per your request. Thanks.

Thanks For the Information.