[SOLVED] How do I search on a join in admin.php


I added a joined field cat.cat_name to the cgridview and cannot search on it.

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







You need to configure sort property of CActiveDataProvider in search method of your model.

UPD: I read not inattentively and think you need sort not search. :(

Do you have an example?

I am still new to yii.


You need to update the search method of the model to include the join:

public function search()


		// Warning: Please modify the following code to remove attributes that

		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->join = 'LEFT JOIN related_tbl as tbl_alias ON tbl_alias.id =  fk_id';


		//... standard criteria here


		$criteria->compare(' tbl_alias.searchable1',$this->tbl_alias->searchable1, true);


		// etc. add the other fields of the relation tbl_alias that you want to access


		return new CActiveDataProvider('MyModelName', array(




This works well for me. There may be a more elegant solution to the join.


I need to do another join, even through I have it joined in relations?


In your models ‘search’ function you probably have a list of criteria compares, like this:

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

If you want to search by a related models field, then include that in the list of criteria:

        $criteria->compare('cat_name', $this->cat_id);

To make that work, put this before the compare conditions:

        $criteria->with = array('cat');

‘with’ is just an elegant way of declaring a join.

You need to include all related tables, of course.

And you might need to prefix some of the field names due to ambuigity.

Still not working. Here is my code.

public $search_location;

public function relations()

  'cat' => array(self::BELONGS_TO, 'Category', 'cat_id'),

$criteria->with = array('cat');

$criteria->compare('cat_name', $this->search_location);

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











Thank you all, It is working now.

I had to add the true word.

I used jacmoe solution.

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

<edit> I saw that you fixed it - great! :) </edit>

I really appreciate the help.

I am coming from a codeigniter background.

I’m coming from CI too, your among friends here!