Yii Framework Forum: Updating count on page view (SQL field=field+1) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Updating count on page view (SQL field=field+1) Couldn't see how to do this anywhere so worked it out... Rate Topic: -----

#1 User is offline   xgarb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 19-July 10

Posted 10 October 2010 - 01:13 PM

In the controller for the view..

$topic->topic_views = new CDbExpression('topic_views + 1');
$topic->saveAttributes(array('topic_views'=>$topic->topic_views));


Hope this helps someone and it's not terrible code.
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 11 October 2010 - 02:25 AM

Hmm. Why not use the updateCounters() method? Or - if you already have fetched the record:

$topic->topic_views++;
$topic->save();





0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users