如何在yii中使用自定义查询进行搜索gridview

时间:2015-03-25 17:59:04

标签: php yii

我使用自定义查询创建了一个简单的gridview。这对我来说很好,但搜索框不起作用。如何使搜索框工作? 这是我的代码:

UserController.php

public function actionAdmin()
    {
                $sql = 'SELECT * FROM user';
                $rawData = Yii::app()->db->createCommand($sql); 
                $count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM (' . $sql . ') as count_alias')->queryScalar();
                $model = new CSqlDataProvider($rawData, array( 
                    'keyField' => 'id', 
                    'totalItemCount' => $count,
                    'sort' => array(
                        'attributes' => array(
                            'id','title', 'type'
                        ),
                        'defaultOrder' => array(
                            'id' => CSort::SORT_ASC, //default sort value
                        ),
                    ),
                    'pagination' => array(
                        'pageSize' => 10,
                    ),
                ));

                $this->render('admin', array(
                    'model' => $model,
                ));
    }

Admin.php(查看文件)

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model,
    'filter'=>$model,
    'columns'=>array(
        array('header'=>'firstname','value'=>'$data["firstname"]'),
        array('header'=>'lastname','value'=>'$data["lastname"]'),
        array('header'=>'username','value'=>'$data["username"]'),

    ),
)); ?>

0 个答案:

没有答案