Yiiでデータを更新する場合、
$model->save();
のようにするとデータがない場合は新規登録(insert)、データがあれば更新(update)するようなSQLが発行される。
ただ、特定のカラムのみ更新したいとか、ちょっと条件をつけて更新したい、カラムをカウントアップしたいという場合は、上記ではできないので、方法を書いておく。
update hoge_table set name='ほげ';
のようにnameカラムだけ更新したい場合は、モデル内で
$this->name='ほげ';
$this->update(array('name'));のようにする。
update hoge_table set name='ほげ' where id=5 and status=2;
のように条件をつけて更新したい場合は、
$this->updateAll(
array(
'name'=>'ほげ',
),
'id=:id and status=:status',
array(
':id'=>5,
':status'=>2
)
));のようにする。
update hoge_table set num=num+4 where id=5 and status=2;
のようにカラムをカウントアップしたい場合は、
$this->updateCounters(
array(
'num'=>4,
),
'id=:id and status=:status',
array(
':id'=>5,
':status'=>2
)
));のようにする。
※参考
CActiveRecord