[Solved] Gridview Filter Doesn't Work If In The Model's Search() There Is A Condition


I discovered a problem about my search method of a model class.

If I implement a $criteria->condition, after this the filter function won’t work in the CGridView. If I comment out the $criteria->condition code line, the filter function will work again in the CGridView.

Why do this with me the Yii? :)

How can I solve this problem?

public function search()


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

		$criteria=new CDbCriteria;











                $criteria->condition = "turn=".Setting::getActualTurn(); // <- this makes won't work the filter function in the CGridView.


                if($this->offset > 0){

                    $criteria->offset = $this->offset;



                if($this->isLimited == true){

                    $criteria->condition .= " AND `saved`='1' ";

                    $criteria->limit = 10;

                    $pagination = false;



                    $pagination = null;



                return new CActiveDataProvider($this, array(


                        'pagination'=> $pagination,



The condition is work well :). And I need it in basic at the admin, or at index.php views.

What do you mean by not working? Is there an error or it doesn’t behave as you think it should?

Why not use same syntax as with other conditions?

$criteria->compare("turn", Setting::getActualTurn());

By specifying "condition" property directly, you are replacing the existing condition that have been constructed using CDbCriteria::compare() with the new condition. So you should use CDbCriteria::addCondition() … or, CDbCriteria::compare() should be much convenient.

BTW, usually you are not supposed to set ‘offset’ and/or ‘limit’ for the criteria in search() method when you are working with CGridView or CListView. It will cause troubles with the pagination.

“What do you mean by not working?”. Not refresh the content of CGridView :), when I search for a specific value of the column in CGridView. Nothing happen. I don’t get any errors, but doesn’t search!

I didn’t know that I can use the compare() for this too :) I thought only for searching/filtering and not for basically view data in the CGridView

So what is the best solution? :) When I would liket to set the ‘offset’ and/or ‘limit’? :) I am here to study.

And thank you to explain me my above problem. Now I understand how the compare() works.

Don’t you want to use the default pager with the CGridView of the admin view (or with the CListView of the index view)?

If you don’t, and if you want to implement a custom pager on your own, then you may have some reason to set ‘offset’ and ‘limit’ to your criteria by yourself.

But usually you don’t need to touch them. Or rather you should not touch them. Leave them to the pagination object of the CGridView or CListView. It will make your development much easier by far.

I hope the following wiki might be interesting to you. :)


Thank you :).

I made a simple webapp. In the admin view I use the simple search (without offset and limit) and the pager.

But at the index view, I use again a CGridView, but without pager!

This is a simple bet game webapp. And at the index.php I view only 10 bet/result on the page, and after 10 seconds I view the next 10 bet/result and again the next 10 result/bet after 10 seconds :), with JavaScript.

This is why I need offset and limit, when I view the index.php.

You know, I always wanted to chat with somebody from Japan. ;D Thanks for the helps.

Ah, the wiki was linked is good. Nice job.

I see. That makes sense.