Search through three db-tables in CGridview


Im trying to create search functionality between three database-tables in a CGridview, but I only get errors when i search.

The relations is a bit messy(but I can’t change the tablestructure):

I start from the Post-model.

The table "Post" is related to the table "Post_Group" that is related to the table "Post_Group_Cat" (contains the different categories).

One post can have several "Post_Group_Cat" connected to itself through "Post_Group".

I guess it’s not possible to display all categories that belongs to one post in CGridview? That’s not so important tho, what’s important is that I want to be able to search in the CGridview for a category.

This is my code:


public $test_search;

public function rules()


			array('..., test_search', 'safe', 'on'=>'search'),



public function relations()


		return array(

			'post_group' => array(self::BELONGS_TO, 'Post_Group', 'post_id'),

			'post_group_cat' => array(self::BELONGS_TO, 'Post_Group_Cat', 'cat_id'),



public function search()


		$criteria=new CDbCriteria;


		$criteria->with = array('post_group' => array('with' => 'post_group_cat'));


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


		return new CActiveDataProvider($this, array(

            'criteria' => $criteria,


                'defaultOrder'=>'t.date_created ASC',


            'pagination' => array(

                'pageSize' => 10,





<?php echo $form->label($model,'test_search'); ?>

<?php echo $form->textField($model,'test_search'); ?>

Any ideas why the search is not working?

Try replacing the $criteria->with with a statement if the secondary table attribute is active from a search:

    if(!empty($this->my_attribute)) {    

        $criteria->addCondition("my Statement");


You must add the variable for your related search parameter at the top of your class file:

    public $my_attribute;

Hi, I solved it in another post

but thank you for the answer tho :)