[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 ^^


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )


Connecting to %s