Update Coutners Of All Results

Hi,

Is there a way I can use updateCounters() to update all results at once?

I want to update the counter ‘visits’ of all Posts I found which has a limit op 10. So update all 10 results with a visit+1.




		$criteria = new CDbCriteria();	

		$criteria->condition = "date_start <= NOW() AND date_end >= NOW()"

		$criteria->limit = 10;

		$criteria->order = "visits ASC";

		

		$posts = Posts::model()->findAll($criteria);


                // Not working...

		$posts->updateCounters(array('visits'=>1));



Thanks in advance.

$posts is an array in your case, so you cannot call method of it.

You can use updateAll with the same criteria.

Thanks a lot, could not find that method. It’s just like PHP, almost for everything there is a function but I always think I have to make it my own. Yet in Yii there is also a lot more then I think on first sight. Only having trouble form time to time to find it.

BTW, I also manage to get the updateCounters working by simply pass the same criteria. For one who’s looking for this:




$posts = Posts::model()->findAll($criteria);

Posts::model()->updateCounters(array('visits'=>1),$criteria);