CGridView & $model->search() additional filters?

Hello community,

I’m using the default admin view to display a model’s records to the user. This view implements a Grid View with ‘$model->search()’ as its data provider, which is pretty cool:

ht-remove-tps://min.us/mbn5IrvzQe (it’s an image, can’t hotlink directly because I don’t have enough posts yet)


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

	'id'=>'category-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'is_imported',

		'belongs_to_user_id',

		...

		array(

			'class'=>'CButtonColumn',

            ),

		),

	),

));

In that view, the user can click on any AR to import it. This basically duplicates the record and updates two fields: belongs_to_user_id (is now the current user’s id) and is_imported (is no true). I got this working perfectly.

Now I only have one problem: The user shouldn’t be able to import their own records or import records that are already imported (non-original). I need to be able to filter some records from displaying. I don’t want to show the records where belongs_to_user_id is equal to the current user’s id, and I don’t want to show the records where is_imported is true.

I found a solution for this but I’m not happy with it:


if( !isset( $_GET[ 'id' ] ) && empty( $_GET[ 'Category' ][ 'id' ] ) ) {

    $model = new Category( 'search' );

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

    if(isset($_GET['Category'])) {

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

    }


    $model->setAttributes( array(

        'is_imported'=>0,

        'belongs_to_user_id'=>'<>'.User::getUserId(),

    ));


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

        'model'=>$model,

    ));

}

This adds some more attributes to the $model, as if the user had entered them himself. This doesn’t really work out, because now the search field for “belongs_to_user_id” shows “<>X”, and the user can’t change it:

ht-remove-tps:// min.us/mbn5IrvzQe (it’s an image, can’t hotlink directly because I don’t have enough posts yet)

Another solution I found is to modify the model’s search method:


public function search()

{

	$criteria=new CDbCriteria;

	$criteria->compare('is_imported',0);

	$criteria->compare('belongs_to_user_id','<>'.User::getUserId());

}

However, this still leaves the problem that the "belongs_to_user_id" search field always shows "<>X".


Is there a solution to that problem? Ideally I would like to filter the $dataProvider to remove some rows, but I understand the rows are collected manually using the model’s search method. Perhaps there’s something like an afterSearch method?

Another approach would maybe be to somehow tell the grid to remove some entries, ideally without the user noticing the filter (no "<>X" in the search field).

Thanks in advance for any help!

Your code in $model->search() is good…

To remove the "<>X" from the filter… remove the code you added in the controller…

If I got you right… above that… you would like the possibility to filter by belongs_to_user_id

For this to work you just need to test in search() if the user wants to filter by that field… something like




if(isset($this->belongs_to_user_id) && $this->belongs_to_user_id!=User::getUserId())

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

else

   $criteria->compare('belongs_to_user_id','<>'.User::getUserId())