Need Advise On Building Dataprovider

I have the following situation:

I am rendering a CGridView of users. However, one of the columns that I would like to have in that grid is a sum of all their hours which can be obtained from a different table as "select sum(hours) from different_table". Can anyone advise me on what is the best approach to include this information within the dataProvider such that it this columns is sortable and I can filter it just like the rest of the native data.



Hi bettor,

The following wiki is for "select count(*) from different_table".

I think you can modify it a bit to meet your needs.

sweet. thanks. The only thing I cannot figure out is how to bind parameters to $criteria->params. $this->name returns NULL. Any idea?

If you are talking about "search" function of a model class, "$this" refers to the model instance that is used for the container of the search parameters, and "$this->name" may be empty when the user has not set any filter by that attribute. So you have to check if $this->name is not empty before you add condition and bind parameter on that attribute.

Or use CDbCriteria::compare() if it’s possible. It will do nothing if the value is empty, and will add condition and bind parameter if it is not. Very convenient.

right! Do you know if it is possible to access some of the cgridview $data in the search()?


"$this" in the search method and "$data" in the CGridView column are generally of the same model class, but they are not the same object instance. They are completely different things.

“$this” in search is usually the same instance of “$model” in the controller and the view. It’s for gathering user inputs. It holds the search parameters which are used in constructing the criteria in the search method.

And “$data” in CGridView is an object in the array that the data provider has fetched from the db. You can’t access them in the search method.

You may want to read this: