November 26, 2008, 1:56am
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.
November 26, 2008, 2:02am
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:
November 26, 2008, 2:27am
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)));
November 26, 2008, 2:38am
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.
November 26, 2008, 3:24am
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.
November 26, 2008, 3:40am
Changed the guide per your request. Thanks.
September 20, 2011, 7:21am
Thanks For the Information.