Search code examples
gridviewyiigridyii1.x

Yii1 Remove initial Search


I'm having problems trying to display a list of elements on CGridView,I have a table with a boolean field is_active who had us 0 default value, so when I want to display this table on CGridView it show just the rows with 0 on is_active field and add 0 on search input by default.

I add this line to search function $criteria->compare('is_active',$this->is_active,false); but stil not working

This is my search function

public function search()
{

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->title);
    $criteria->compare('description',$this->description);
    $criteria->compare('is_active',$this->is_active,false);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

This is my CGridView

...
{
  'header' : 'Activated',
  'filter' : CHtml.activeTextField( model, 'is_active' ),
  'value' : '$data->is_active ? "Oui" : "Non" '
}
...

This what I got

enter image description here

But wat I want is to show all table's elements.


Solution

  • You need to use unsetAttributes() to reset default values detected from database schema:

    $model = new MyModel('search');
    $model->unsetAttributes();
    if (isset($_GET['MyModel'])) {
        $model->attributes = $_GET['MyModel'];
    }
    $dataProvider = $model->search();