Dropdown Filter In Cgridview Based On Condition


I have a gridview of courses using 2 related models. I’ve managed to get filtering working by putting properties in the main model, putting them in the rules as safe on search and then referencing them in the search method criteria. For one of the columns which is from the related model, I have a dropdown as follows:


            'header'    => 'ECTS',

            'name'      => 'search_ects_points', // property added to CoursePeriod model

            'value'     => '$data->course->ects_points', // related model

            'filter'    => CHtml::listData(CourseEcts::model()->findAll(), 'ects_points', 'ects_points'), // for the dropdown pulling from CourseEcts model


This pulls all possible ECTS points from the model CourseEcts which is related to Course (Course.ects_points belongs to CourseEcts.ects_points).

The dropdown displays and filters perfectly, however my gridview has a condition to only show courses by the current year. This year there will only be 2 possible ects points (5 and 10) but the dropdown shows 5, 6, 10 and 15 which are all possible ects points in the database.

Is there a way to make the dropdown only show ects points based on this condition? Basically filtering the filter. :slight_smile:

I haven’t supplied any code to keep the post short, but if you need some code to see how everything is set, just let me know.


 CHtml::listData(CourseEcts::model()->findAll("email_id=:email_id",array(':email_id' => $id)), 'ects_points', 'ects_points'), 

Please look the condition inside the findAll, This may help u…

Hi Balu

Thanks for replying!

I’m not sure what “email_id” and $id would be refering to, the model CourseEcts only has one property -> ects_points which is therefore the primary key.

Course.ects_points is a foreign key to CourseEcts.ects_points and CoursePeriod.course is a foreign key to Course.course_id.

Does this make sense to you?


For that you should set relation among the models


this will help you…

Hi again

Apologies, your first answer was actually the closest.

This worked for me.

'filter'    => CHtml::listData(CoursePeriod::model()->findAll("year=:year",array(':year' => $year)), 'course.ects_points', 'course.ects_points'),

I just needed to change the model to the one used in the GridView and use the relations that I had already built.

I’m not sure about the performance of it, but so far so good! :slight_smile:

Thanks for all your help and suggestions.