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

Revision #5 has been created by marcovtwout on Jan 26, 2015, 4:08:12 PM with the memo:

replace and simplify confusing example
« previous (#4)

Changes

Title unchanged

Using counters with ActiveRecord

Category unchanged

Tips

Yii version unchanged

Tags unchanged

activerecord, counters, savecounters, updatecounters

Content changed

[...]
}
```

With this, only the visits column will be updated and the validation will not be triggered.

If you're not using the 1.1.8 yet, there's the [CActiveRecord::You can also update multiple models at once by passing a custom criteria to updateCounters()] method: ```php public function actionView($id) {  $post = Post::model()->findByPk($id);
 
    $post->
updateCounters( array('visits'=>1), array('condition' => "id = :id"), // or another, broader condition array(':id' => $this->id),
);
$this->render('view', array('post' => $post));
[...]
```

The piece of code above does exactly the same that saveCounters does. Note that updateCounters has two additional parameters. After the column used to count, we pass a condition to tell which record(s) should be updated. If you used any paramater in this condition (like the ":id"), you should pass the parameter value in the third argument of the method.
 
 
**Important: If you do not pass a condition, all records will be updated.**
11 1
17 followers
Viewed: 45 561 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