Search() & Filter of a relations field through CGridView

anyway, its now working… maybe somewhere was the mistake…

I have the same type of problem. I have three tables tbl_sale, tbl_stock and tbl_products. Tables are linked with each other in following way:

[size="3"][b]tbl_sale.id link to tbl_stock.sale_id

tbl_stock.product_id link to tbl_product.id[/b][/size]

Sale 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(

				'client_relation' => array(self::BELONGS_TO, 'Clients', 'id'),

 				'stock_relation' => array(self::BELONGS_TO, 'Stock', array('id'=>'master_id')),//used in admin.php

 				'product_relation' => array(self::BELONGS_TO, 'Products', array('product_id'=>'id'), 'through'=>'stock_relation'),

 		);

	}


public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


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

                $criteria->with = 'stock_relation';

		$criteria->together = true;

		$criteria->addSearchCondition('stock_relation.sale_id',$this->id,true);

return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

				'sort'=>array(

						'attributes'=>array(

								'sale_date', 'client_id','discount', 'productid'

						),

				),//*/

				'pagination'=>array(

						'pageSize'=>Constants::PAGE_RECORDS_LIMIT,

				),

		));

	}



admin view




$this->widget('bootstrap.widgets.TbGridView', array(

		'type'=>'striped bordered condensed',

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

	    'template'=>"{items}{pager}",

		'filter'=>$model,

		'columns' => array(

				array(

						'name'=>'productid', 'header'=>'Product',

						'value' => '$data->product_relation->name',//'Products::model()->findByPk($data->stock_relation->product_id)->name',

						'filter'=>CHtml::listData(Products::model()->findall("edt IS NULL ORDER BY name"), 'id', 'name')

				),

				array(

						'name'=>'sale_quantity', 'header'=>'Sold Qty',

						'value' => '$data->stock_relation->stock_out',

				),

				array(

						'name'=>'sale_price', 'header'=>'Sale Price',

						'value' => '$data->stock_relation->sale_price',

				),

				

			array(

						'class'=>'bootstrap.widgets.TbButtonColumn',

						'htmlOptions'=>array('style'=>'width: 50px'),

						//'template'=>'{view}{update}{delete}',

						'buttons'=>array(

								'delete' => array(

										//'url'=>'Yii::app()->controller->createUrl("ports/delete", array("id"=>$data[id],"command"=>"delete"))',

										'visible'=>'false'

								),

						),

				),

		),

));



When i filter gridview by product then it is not filtering. How can i do that in sale search function.

Hi all.

Thank you for examples.

I think it is important - to decide what exactly we want to find for filter. Relation value or value that connect.

If we want search value that connect we have to do:


public function search(){

...

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

...

}

and in view


'columns'=>array(

  array(

    'filter' => CHtml::listData(SomeModel::model()->findAll(), 'id', 'id_name'),

so we don’t need public attribute.

If we want search by value from relation table


public function search(){

...

  $criteria->compare('relationname.id_name', $this->relation_id_name,true);

...

}

in view


'columns'=>array(

  array(

    'filter' => CHtml::activeField($model, 'relation_id_name'),