Thursday, March 6, 2014

yii-model-fetch-row-by-primary-key

Yii model fetch Row By primary Key Example

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

we need to fecth a particular row based on primary key value.

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

echo CJSON::encode($datas);

[{"id":"1","account_name":"New 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 get acount_name that have a primary key value is 1.

lines are,


$pk=1;
$single = Account::model()->findByPk($pk);
echo $single->account_name;


// output is New Tea

No comments:

Post a Comment