How To Filter Data In Cgridview When Records Get From Different Models

Here Is my Code:


class ProductController extends Controller



 * Manages all Skus.


public function actionAdmin()



        $uid = Yii::app()->user->getId(); 

        if($uid != 1){

            $value = ComSpry::getUnserilizedata($uid);

            if(empty($value) || !isset($value)){

                throw new CHttpException(403,'You are not authorized to perform this action.');


            if(!in_array(1, $value)){

                throw new CHttpException(403,'You are not authorized to perform this action.');



        $image-new Skuimages;

	$model=new Sku('search');


            $model->unsetAttributes();  // clear any default values













public function search()


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

	// should not be searched.

	$criteria=new CDbCriteria;


                  $criteria->condition="totmetalwei > :met_not_less";




                  $criteria->condition="totmetalwei < :met_not_more ";


























            if(!empty($this->type)) $criteria->with[] = 'skucontent';

            if(!empty($this->gemstone)) $criteria->with[] = 'stones';

            if(!empty($this->gem_shape)) $criteria->with[] = 'shapes';

            if(!empty($this->gem_size)) $criteria->with[] = 'sizes';

            if(!empty($this->type) || !empty($this->gemstone) || !empty($this->gem_shape) || !empty($this->gem_size)) $criteria->together = true;


            if(!empty($this->type)) $criteria->compare( 'skucontent.type', $this->type, true );

            if(!empty($this->gemstone)) $criteria->compare('stones.namevar',$this->gemstone, true);

            if(!empty($this->gem_shape)) $criteria->compare('',$this->gem_shape, true);

            if(!empty($this->gem_size)) $criteria->compare('sizes.size',$this->gem_size, true);

            if(!empty($this->sku_size)) $criteria->compare( 'skucontent.size', $this->sku_size, true );


	return new CActiveDataProvider(get_class($this), array(





In the view, I am getting ajax error when I search shape with size and stone name.

Many Thanks…



Can you show the rest of the response? The content of the exception message should tell you what’s gone wrong.

Thanks for your replying, Please send me the solution.

Many Thanks.



That’s still not showing the exception details. You’ll need to capture the response in your browser’s developer tools.

If you’re using chrome, go to Tools -> Developer Tools, then click on the network tab.

Run your filter request again and you should see an extra line appear in the network table, which will probably be red because an exception was thrown.

Click the row in the left hand cell, then choose the Response tab.

Paste everything in there into here.

Thanks So much in advance.

Please check the attached snap.



For some reason tbl_stone is being joined twice with the same alias. Something might be wrong with your relations. Can you post the relations for the base model here?

hii Keith, Thanks for your suggestion, Here is my code of model:

public function relations()


	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'iddept0' => array(self::BELONGS_TO, 'Dept', 'iddept'),

		'idpo0' => array(self::BELONGS_TO, 'Po', 'idpo'),

		'refrcvd0' => array(self::BELONGS_TO, 'Dept', 'refrcvd'),

		'refsent0' => array(self::BELONGS_TO, 'Dept', 'refsent'),

		'idstone0' => array(self::BELONGS_TO, 'Stone', 'idstone'),


                    'sizes' => array(self::HAS_MANY, 'Stonesize', array('idstonesize'=>'idstonesize'), 'through'=>'idstone0'),

                    'shapes' => array(self::HAS_MANY, 'Shape', array('idshape'=>'idshape'), 'through'=>'idstone0'),

                    'iduser0' => array(self::BELONGS_TO, 'User', 'updby'),



Hey Keith, You are the Masterman in yii…

Many Thanks, My problem is solved.

And the answer is??????

Check this wiki: Use CGridview to sort and filter CSqlDataProvider - when used with UNRELATED tables (or complex queries)