How to customize CGridView when some data comes from FK (relations)


There is a table for districts and another table for (parent) cities/regions. The relation between the 2 tables is ONE-TO-MANY (regions-to-districts) as defined in relations() of the 2 Model classes as follows:


'region' => array(self::BELONGS_TO, 'CityRegion', 'region_id'),


'cityDistricts' => array(self::HAS_MANY, 'CityDistrict', 'region_id'),

'cityDistrictsCount' => array(self::STAT, 'CityDistrict', 'region_id'),

The relation works just fine and all CRUD functions work as well as the additional count of all districts assigned to cities/regions. The only minor problem is the admin view of '‘CityRegion’ where the ‘# of districts’ is listed in 1 column, too. See image below. Questions:

  1. How can I get rid of the “0” (see the highlighted input box); it’s 0 by default.

  2. How could I possibly get this column to be sortable? For by now it’s not. The other columns work just fine.

Maybe there is a simple solution and I don’t see the forest for the trees… sigh.

Thanks for your help.

To remove the default values you can use unsetAttributes()…

As for the sort… if that is a STAT relation… I’m not sure if it can be done as this relation generates a separate SQL command (there was a discussion about this already in the forum)

unsetAttributes() is what I was looking for! The default value disappears nicely.

Unfortunately it’s true that a STAT relation cannot be filtered and is not sortable at all. Which leads to the question if the one really needs to see the filter field of this column at all? Setting the filter to false does the trick here and lets the entire filter filed disappear, too.

Thanks, mdomba!