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:// (it’s an image, can’t hotlink directly because I don’t have enough posts yet)

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














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(








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:// (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;




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