Thursday, March 6, 2014

yii-model-limited-rows

Yii model fetch limited rows 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 to display only limited rows.

Lines are a follows,

$criteria = new CDbCriteria;
$criteria->limit = "3";
$datas = Account::model()->findAll($criteria);

echo CJSON::encode($datas);

ouput is

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

No comments:

Post a Comment