Search across related models via CGridView

I’m trying to create an Admin section that will allow admins to search over a model (“Child”), which is related to another model called “Parent”.

But I can’t figure out:

  1. How to pass in the related model (“parent”) in the filter, so admins may search in the textfield that’s rendered

  2. How to allow admins to use the Advanced Search for the parent

My view:




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'classes-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

        'pager'=>array('class'=>'CListPager'),

	'columns'=>array(

	  'id',

          //'title',

	  'parent.title:text:Parent Title',

	  'price:text:Price',

	),

)); ?>



My relations function in "Child":




public function relations()

{

	return array(

           'parent' => array(self::BELONGS_TO, 'ParentModel', 'parent_id'),

	);

}



My search function in my "Child" model:




public function search() {


	$criteria=new CDbCriteria;

        $criteria->with = array(

            'parent'=>array(

                'select'=>'title'

            )

        );

        //$criteria->addSearchCondition('parent.title',$this->parent->title);


	$criteria->compare('id',$this->id,true);

        //$criteria->compare('parentTitle',$this->parentTitle,true);

        $criteria->compare('price',$this->price,true);


	return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria,

            'pagination'=>array('pageSize'=>50),

	));

}



I’ve even played around with declaring:

public $parentTitle=’’;

as a variable of my Child model class.

Ideas?

This has already been discussed a couple times, please check this for relevant topics about it.

Thanks. Yea, I searched before I posted, but still couldn’t figure it out… so finally decided to post.

Turns out, I tried a solution previously posted (that should have worked) but it didn’t work for me because I didn’t put the column ‘value’ in single quotes. I knew it was some small thing that I overlooked.




array(

  'name'=>'parentTitle',

  'filter'=>CHtml::activeTextField($model,'parentTitle'),

  'value'=>'$data->parent->title',

),



Thanks. All good now!