Difference between #3 and #4 of
Using counters with ActiveRecord

Revision #4 has been created by Alexander Ustimenko on Jul 16, 2012, 6:42:31 AM with the memo:

Adding 3rd problem -- updating in parallel by different user.
« previous (#3) next (#5) »

Changes

Title unchanged

Using counters with ActiveRecord

Category unchanged

Tips

Yii version unchanged

Tags unchanged

activerecord, counters, savecounters, updatecounters

Content changed

[...]
$post->save();
$this->render('view', array('post' => $post));
}
```

We have t
wohree problems with this approach. All we want is to update the visits column, but the entire record will be updated. Also, if we didn't pass the false argument to the save method, all the validation process will be executed. Also if someone in parallel will update record from 4 to 5, we still save it as 5, as our version of post record have 4.

Since 1.1.8, the CActiveRecord class has a method that can help us with this task. It's the [CActiveRecord::saveCounters()] method and its usage is pretty simple:


```php
[...]
11 1
17 followers
Viewed: 45 304 times
Version: 1.1
Category: Tips
Written by: davi_alexandre
Last updated by: marcovtwout
Created on: Dec 2, 2011
Last updated: 9 years ago
Update Article

Revisions

View all history