CActiveRecord and CGridView adding columns

Hi everybody, I started to use yii a few weeks ago and it seems to be very nice!

I found out quiete a lot on my own, but now I have a problem I couldn’t find a solution.

In my mysql database are two tables, one is a link table where I store a link together with an link_id.

In the other table I store the overall clicks (not of the day, but added) on the link. So the table has three columns: count, timestamp and link_id.

I use a CGridView to display the content of the link table. Right now it just displays the link and the link_id, but I want to add the growth of the clicks in the periods of 3, 7 and 30 days to the view. Where can i tell the CGridView or the model that it should load the 4 click counts and calculate the growth out of it? ((3DaysAgoClicks / todayClicks) * 100) Does it make sense to calculate this? or should I create an extra table where I store these information? I have up to 1000 links which get clicked quiete a lot and the overview page is also clicked pretty often…

Thanks for the help!

Can you post your filter search conditions and model relations ?

You can add some new properties in the link model.

To achive it, just create a function named getPropertyName, for example:

public function get3DaysAgoClicks()


  /*calculate clicks*/

  return $numberOfClick;


This property can be accessed as $link->3DaysAgoClicks, and also you can configure zii gridView like that:







Thank you for the fast help!

The model relation and search in the link class is as followed.

public function relations()


		return array(




public function search()


		$criteria=new CDbCriteria;



		$provider = new CActiveDataProvider(get_class($this), array(






                return $provider;


Does it load all the counts when I use the link model? How can I access specific counts (3 days, 7 etc ago?).

Thank you zaccaria for your idea, this seems to be a good idea. Will I then be able to sort the table by the value?

Not tested :)

use date criteria condition like


in controller

public function getdate($date,$specialtime){

$strdate = strtotime($date);

$newtime = $strdate - $specialtime;

return  date('Y-m-d', $newtime);


and then in your filter

$this->widget('zii.widgets.grid.CGridView', array(











        'filter'=>CHtml::activedropDownList($model,'date',array('$this->getdate($data->date,7*24*60*60)'=>'7 Days','$this->getdate($data->date,3*24*60*60)'=>'3 Days'),array('prompt'=>'Choose date')),


I have tested this in the following way:

        public function getCurrentCount() {

            $counts = LinkCounts::model()->current()->find('id_link='.$this->id);

            return $counts->count;


$this->id does not seem to work, how can I access the id of the current link?

Thank you so much!