Custom Value Calculation for ListView Item

Hi all,

I want to to display some data in a ListView. In my case the data is bound to the grid by using the CActiveDataProvider. Now the issue is, that I retreive the Birthday but want to display the Age of the User in the ListViewItem.

What is the best way to calculate this data and show it in every ListView entry? Possible solution is, doing the calculation within the view but this is not very nice…

any code suggestions?



do it in your model:

public function getAge(){

 // calculate it   here you can refer to $this. and can directly access this var $this->birthDay.....


in your gridview or listView you see the $data represent your model

so you can use $data->getAge() to display it ($data->age; also ok).

by the way if you use sqlDataProvider the $data variable represents a row of you sql query result set,if you want to do like above ,just create a static method for it in your model Class say User

           class User extends CActiveRecord{

              public static function getAge($birthDay){

       // calculate it with  $birthDay variable


   // access it like :



make sure the logic is always in your model class , don’t scatter it to view files or controller . you can refer to this wiki for some another way Render customized/complex datacolumns