X
X
Xintrea2014-10-05 13:52:54
Yii
Xintrea, 2014-10-05 13:52:54

Yii 1.x: Why doesn't sorting work in CActiveRecord?

I'm trying to get data from the model by sorting it by the sort_order field. The code is like this:

// Данные меню
$menu = Menu::model()->findAll('', array('order'=>'sort_order') );

However, the data is returned unsorted. When I iterate over the result:
foreach($menu as $item)
  echo $item['text'];

Then I see that the data is received as it is located in the database, without sorting by the sort_order field.
What's wrong?
I use the documentation here: www.yiiframework.com/doc/api/1.1/CActiveRecord#fin...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Bezrukov, 2014-10-05
@bezrukovPS

As far as I remember, it should be like this:

$menu = Menu::model()->findAll(array('order'=>'sort_order'));

D
Dmitry Bay, 2014-10-05
@kawabanga

Try it through CDBcriteria, it will be easier to understand the code later:

$criteria = new CDbCriteria;
$criteria->order = 'sort_order DESC';

$model = findAll($criteria);

A
Alexander Zelenin, 2014-10-05
@zelenin

three days ago the question arose how to bind to AR orderby. Driven into Google https://www.google.com/search?q=yii+findall+orderb...
on the first link the correct example. What's stopping you from doing so? it will take about 15 seconds.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question