Filter A Cactivedataprovider Based On A Computed Property

Hi everyone,

I try to explain my problem the best I can, because I’m really stucked atm. I would like to display a filtered GridView. The filter criteria is however a computed value.

I’ve got an Invoice-model, which has several positions (seperate model, called InvoiceDetail).

I’ve added a property to the invoice model, which has no corresponding field in the database (it displays the amount that is left for the invoice to be paied, the field is called “amountOpen” and is populated in the AfterFind()-method of the invoice model).

Now, I’m wondering how I can filter this data. CDbCriteria won’t work, because it would generate a WHERE-clause, which is useless, because I have no field called “amountOpen” in the database.

Is there a way to filter a cActiveDataProvider after the models got fetched?

It’s doable, but not a good idea.

You can use the getData() method, alter it and put back into the data provider using setData(). But if you filter out data this way and have pagination turned on in the data provider it will break, you will be left with less rows than page size is.

If you want to use a pre-calculated value to do the filtering you would need to calculate it for every model, gather their primary keys in an array and use that in a IN condition in the query.

Because it seems to be too costly, you probably have to add such column in the tables and update its value whenever any of the InvoiceDetail data changes. You could do that with triggers in the database or beforeSave/afterSave in models.