[yii1] Create Search for related model in GridView

Today I challenge myself to make a documentation of my code. And the topic of today’s post is how to create search (filter) for related model in your GridView (using yiiframework).

First, we’re working in model:

public $CourseIDFK; // put your target field’s name as a public variable

then, make a function, let’s name it search2().

public function search2() {

$criteria = new CDbCriteria;

$criteria->together = true;
$criteria->with[] = ‘courseIDFK’;

if ($this->CourseIDFK) {

$criteria->compare(‘courseIDFK.Name’, $this->CourseIDFK, true);


return new CActiveDataProvider($this, array(
‘criteria’ => $criteria,


then in controller put ‘search2’ as a parameter of the model’s object :
$model=new Classregistration(‘search2’);

in view admin, set dataprovider as below:

and don’t forget to put your target’s field value

‘value’ => ‘($data->courseIDFK->Name)’, // this is your target field’s value

voila! now you can search any value of your related model in your GridView 😉

Still can’t figure out? ask me in comment section. Sure I will help u ^^


Halo dunia!

Hai! I’m a twenty-something coder girl who has a giant dream. I want to remember all of the logics that I wrote in my yii based application. Don’t hesitate to ask anything that related to my blog post 🙂

Ciao bella!