I have a model of a visitor. In this model and in the database there is a field date of birth. Sometimes I need the age of the visitor, but I dont want to store this value in the database because I would continously need to update it. So I added a variable to my model class called age. Using the afterFind method of the model class I fill the age variable with the correct age based on the date of birth. This works fine in all views (DetailView, Gridview). The only problem is, I would like to sort the GridView based on age. When enabling sorting, all column headers are sortable except the age column. When adding age to the rules -> safe on search array, I get an SQL error, column not found. Clearly Yii searches the database table for the column, which its not going to find.
Is there a way I can achieve sorting on age? Perhaps I need to define the age variable in another way?
The only thing that doesn’t work is when I search for a specific value by entering it in the textfield in the header column. If I understand you correctly I will not get this to work because this field searches directly in the DB.