CGridview过滤器似乎不起作用

时间:2012-12-17 17:15:23

标签: filter yii cgridview

有人可以帮助我使用我的过滤器,他们似乎没有从数据库中获取数据。

查看具有CGridview

的内容
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'enablePagination'=>true,
'pager'=>array(
    'maxButtonCount'=>'7',
),
'columns'=>array(
    array(
        'name'=>'bt_number',
        'type'=>'raw',
        'value'=>$model->bt_number,

    ),
    array(
        'name'=>'date_time',
        'type'=>'raw',
        'value'=>$model->date_time,
    ),
            array(
        'name'=>'broker',
        'type'=>'raw',
                    'value'=>$model->broker,
        'filter'=>Yii::app()->params['brokers'],

    ),
    array(
        'class'=>'CButtonColumn',
        'template'=>'{view}{update}'
    ),
)
));

为模型建模,我试图打印出一个我过滤的搜索元素,而不是任何显示

public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    echo "booker ".$this->broker;// exit;
           //above only displays booker there is nothing in $this->broker

    $criteria=new CDbCriteria;

    $criteria->compare('bt_number',$this->bt_number);
    $criteria->compare('sign',$this->sign,true);
    $criteria->compare('fm_buys',$this->fm_buys,true);
    $criteria->compare('fm_buys_amt',$this->fm_buys_amt,true);
    $criteria->compare('against',$this->against,true);
    $criteria->compare('bt_sett_date',$this->bt_sett_date,true);
    $criteria->compare('bt_order_type',$this->bt_order_type,true);
    $criteria->compare('date_time',$this->date_time,true);
    $criteria->compare('dealer',$this->dealer,true);
    $criteria->compare('rate',$this->rate,true);
    $criteria->compare('broker',$this->broker,true);
    $criteria->compare('recapped',$this->recapped,true);
    $criteria->compare('settled',$this->settled,true);
    $criteria->compare('sett_date',$this->sett_date,true);

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

Controller没有从$ _GET var

获取任何内容
public function actionIndex()
{
    $model=new BrokerTrades('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['BrokerTrades']))
        $model->attributes=$_GET['BrokerTrades'];

    print_r($_GET)  ;
    $this->render('index',array(
        'model'=>$model,
    ));
}

我无法理解为什么这个过滤器不起作用请帮助。

1 个答案:

答案 0 :(得分:2)

确保要过滤的每个字段在模型规则中设置为安全。

public function rules(){
  return array(
    ..
    array('bt_number,sign,fmbuys','safe','on'=>'search'),
  );
}

需要将属性设置为安全,以使此行正常运行:

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

只需在第一个字符串中包含您要搜索的所有属性,以逗号分隔。

看看这是否有帮助。