- Model->save();
- Model->saveCounters();
- Model->updateCounters();
For all these methods, we have to get the object before performing the updating process. i.e.,
$obj = YourObject->model()->findByPk($id);
Difference among them:
1.$obj->visits += 1;
$obj->save();
2.$obj->saveCounters(array('visits'=>'1'));
3.$obj->updateCounters(array('visits'=>'1', 'id'=>$id));
The Trick is:
- Better to use saveCounters()!
- If you use updateCounters(), pls make sure you have put id in a condition as highlighted in the code. Otherwise, the 'visits' field for all records +1.