Gridview filtering by nonexistent db-column / virtual attribute

Hi all!

I have a small problem with the CGridView filter. I’m using a regular grid view and can filter all columns, also across relations. However, my model has a virtual attribute that needs to be calculated every time the model is accessed. I have a [font=“Courier New”]getMyPropery()[/font] method, that returns [font=“Courier New”]true[/font] or [font=“Courier New”]false[/font]. In my grid view I want to filter by this property.

Now, I have a regular [font=“Courier New”]search()[/font] function and use [font=“Courier New”]CDbCriteria[/font] to compare the db columns and virtual attributes. This doesn’t work for my [font=“Courier New”]getMyPropery()[/font] method though, because there is no database column to compare.

Is this even possible? And if so, how?


When using CGridView you are limited to sorting by something that can be represented by an SQL statement.

This is because the CActiveDataProvider uses a CSort to generate the ORDER statements. CSort gives you ways to sort virtual attributes but only when it can be represented as an SQL statement. The CSort documentation explains this.

You could probably write an implementation of CActiveDataProvider that could sort the models in the current page only based on a virtual attribute. You’d probably need to extend a fair bit of code.


I’ll have to solve it with a very, very complex SQL statement then.