Thursday, March 6, 2014

Yii-fetch-rows-order-by

Yii model order by column example.

Assume that we have a model with two columns, they are id and account_name

$datas = Account::model()->findAll();

echo CJSON::encode($datas);

[{"id":"1","account_name":"Tea"},{"id":"2","account_name":"Food"},{"id":"3","account_name":"Travel"},{"id":"4","account_name":"Pettycash"}]

The above line shows the values in the model.

we want show values that are ascending order by account_name column;

Lines are as follows,

$criteria = new CDbCriteria;
$criteria->order = "account_name asc";
$datas = Account::model()->findAll($criteria);

echo CJSON::encode($datas);

// output is

[{"id":"2","account_name":"Food"},{"id":"1","account_name":"New Tea"},{"id":"4","account_name":"Pettycash"},{"id":"3","account_name":"Travel"}]

No comments:

Post a Comment