Thursday, March 6, 2014

Yii-update-model-by-primary-key

Yii model Update Row By primary Key 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 update  account_name by Primary key.

line are as follows,

$pk=1;

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

$single->account_name = "New Tea";  // actual value is Tea

$single->save();

after These lines we got following value. while checking.....

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

echo $single->account_name;

// output is New Tea

No comments:

Post a Comment