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

Here Is my Code:

Controller:

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


	if(isset($_GET['Sku']))


		$model->attributes=$_GET['Sku'];


                    $imageUrl=$image->imageThumbUrl;   


	$this->render('admin',array(


		'model'=>$model,


                    'skuimages'=>$imageUrl


	));


             


}

}

}

MOdel:

public function search()

{


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


	// should not be searched.





	$criteria=new CDbCriteria;


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


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


                  $criteria->params[':met_not_less']=$this->met_not_less;


            }


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


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


                  $criteria->params[':met_not_more']=$this->met_not_more;


            }


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


            


            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('shapes.name',$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(


		'criteria'=>$criteria,


	));


}

Views:

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

Many Thanks…

3830

error.JPG

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.

3831

error2.JPG

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.

3832

error3.jpg

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)