Filter on gridview from related model

Hi

I have an application with companies and documents from those companies

With the help of gii i have created an MVC to CRUD companies and documents and customized it to my needs.

One change i made was the ability to show in a gridview in the company view the documents related to that company.

This way when i view a company i can see the companies documents.

I managed to do this using a function in the documents model to fetch the documents from a specified company (See code bellow)

Company View Code




<br /><h2> <?php echo Yii::t('app','These documents belongs to this Company:') ?> </h2>

<?php

    $locale = CLocale::getInstance(Yii::app()->language);


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

        'id'=>'documents-grid',

        'dataProvider'=>Documents::searchByCompanyId($model->id),

        'columns'=>array(

            array(

                'name'=>'id_doccategory',

                'value'=>'CHtml::value($data,\'idDoctype0.idDocCategory0.name\')',

            	'header'=>Yii::t('app','Document Category'),

            ),

            array(

                'name'=>'doccategory_periodic',

                'value'=>'($data->idDoctype0->idDocCategory0->periodic) ?  Yii::t(\'app\',\'Yes\') : Yii::t(\'app\',\'No\')',

            	'header'=>Yii::t('app','Periodic'),

            ),

			array(

                'name'=>'id_doctype',

                'value'=>'CHtml::value($data,\'idDoctype0.name\')',

            	'header'=>Yii::t('app','Document Type'),

            ),

            array(

                'name'=>'year',

                'header'=>Yii::t('app','Year'),

            ),

            array(

                'name'=>'file_size',

                'header'=>Yii::t('app','File Size (bytes)'),

            ),

            array(

				'class'=>'CButtonColumn',

				'header'=>'',

            	'template'=>'{download}',

            	'buttons'=>array(

            		'download'=>array(

            			'label'=>Yii::t('app','Download'),

		                'url'=>'Yii::app()->createUrl("client/displayDocument",array("id"=>$data->id))',

            		),

            	),

            ),

        ),

        'htmlOptions'=>array('style'=>'cursor: pointer;'),

        'selectionChanged'=>"function(id){if ($.fn.yiiGridView.getSelection(id) != '') {window.location='" . Yii::app()->createUrl('client/displayDocument/id') . "' + '/' + $.fn.yiiGridView.getSelection(id);}}",

    ));




Documents Model Function




	public function searchByCompanyId($id_company)

	{

		$criteria=new CDbCriteria;


		$criteria->compare('id_company',$id_company);


                $criteria->order = "year";

                

		return new CActiveDataProvider('Documents', array(

			'criteria'=>$criteria,

		));

	}



I have two problems:

[list=1]

[*]Sorting does not work properly

[*]Filter does not work properly

[/list]

To use filtering i use:




'filter'=>Documents::model();



I see the call being made in web server access log but nothing changes

Help anyone ?

list what you want in the filter. If you want to filter a function that exists in the model, do so:




'filter'=>Documents::model()->ListName();



if not do so:




'filter' => array('yes' => 'YES', 'NO' => 'NO'),



and do it




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

        'id' => 'colaborador-grid',

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

        'filter' => $model,



Maybe this will help GridView Filtering of Relational Data ring-of-relational-data/