yii 如何通过对CGridView的查询访问数据

j0pj023g  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(139)

我试图在我的视图上使用CreateCommand所做的搜索来填充我的CGridView,但我仍然需要将CActiveDataProvider对象作为dataProvider传递。
有没有办法将CreateCommand对象转换为CActiveDataProvider,或者直接作为CActiveDataProvider执行相同的查询?
这就是

<?php $this->widget('zii.widgets.grid.CGridView', array(
                    'id' => 'credenciado-grid',
                    'dataProvider' => $model->search(),
                    'filter' => $model,
                    'afterAjaxUpdate' => "loadAfterAjax",
                    'rowHtmlOptionsExpression' => 'array("style"=>"cursor: pointer","data-row-id" => $data->cre_id, "class" => "row_grid")',
                    'columns' => array(
                            'cre_razao_social',
                        array(
                            'header'=>'Status',
                            'name'=>'cre_ativo',
                            'value'=>'Status::getStatus($data->cre_ativo)',
                            'filter'=>array(true => 'ATIVO',false => 'INATIVO'),
                            'filterHtmlOptions'=>array('class'=>'col-sm-2'),
                        ),
                    ),
                    )); ?>

这是控制器

$model = new Credenciado('getValoresCredenciadosByConvenioRank');
        $model = new CArrayDataProvider($model, array());
//        pe($model);
        $modelEst = new Estabelecimento('search_adm');
//        pe($model);

        $modelEst->unsetAttributes();
        if (isset($_GET['Estabelecimento']))
            $modelEst->setAttributes($_GET['Estabelecimento']);

//        $model->unsetAttributes();
//        if (isset($_GET['Credenciado']))
//            $model->setAttributes($_GET['Credenciado']);

        $this->render('listar',array(
            'model' => $model,
            'modelEst' => $modelEst,
            'mes1' => $mes1,
            'mes2' => $mes2,
            'meses' => $meses,
            'produto' => $produto,
            'data' => $format,
            'datas' => $date_unique,
            'series' => $series,
            'ano1' => $ano1,
            'ano2' => $ano2
        ));
    }

这是模型

$q = Yii::app()->db->createCommand()
            ->select("
            cre.cre_razao_social,
            count(op.ope_id) as linhas,
            par.par_data_vencimento,
            pro.pro_codigo,
            SUM(
                CASE
                    WHEN date_trunc('month',par_data_vencimento) < date_trunc('month',now()) AND pro_codigo != 2 THEN
                        par.par_valor
                    WHEN pro_codigo = 2 THEN
                        op.ope_valor_parcela
                    ELSE
                        op.ope_valor_parcela
                END
            ) as total_carteira")
            ->from("operacao op")
            ->join('parcela par', 'par.par_operacao_ope_id = op.ope_id')
            ->join('pos po', 'po.pos_id = op.ope_pos_pos_id')
            ->join('rubrica_credenciado rub', 'rub.rub_id = po.pos_rubrica_credenciado_rub_id')
            ->join('credenciado cre','rub.rub_credenciado_cre_id = cre_id')
            ->join('produto pro', 'pro.pro_id = rub.rub_produto_pro_id')
            ->join('matricula ma', 'ma.mat_matricula = op.ope_matricula AND ma.mat_convenio_con_id = '.Yii::app()->user->convenioid.' ')
            ->where('op.ope_con_id = :con_id', array(':con_id' => $con_id));
        $q->andWhere(
            "(par.par_data_vencimento between :datamin and :datamax and ope_status in('E','S'))
            OR
            (par.par_data_vencimento between :datamin and :datamax AND par.par_num_parcela = op.ope_qtde_parcelas AND op.ope_status = 'Q')
            ",
            array(':datamin'=>$datamin, ':datamax'=>$datamax));

        if(Yii::app()->user->sistema != 1) {
            $q->andWhere('rub_consignavel = true');
        }

        $q->order('par.par_data_vencimento', 'ASC');
        //$q->andWhere(array('in', 'ope_status', array('E','S')));

        $q->group('cre.cre_razao_social, pro.pro_codigo, par.par_data_vencimento');

        $q->queryAll();

这是错误

exception 'CException' with message 'CArrayDataProvider and its behaviors
do not have a method or closure named "search".' in
/var/www/html/yii/framework/base/CComponent.php:266
Stack trace:
mqkwyuun

mqkwyuun1#

您在视图中犯了一个错误。在控制器中,您已经在传递CArrayDataProvider对象(即$model)到您视图,因此不应对$model变量调用search()函数。相反,您应该从模型中的CreateCommand创建CArrayDataProvider对象,并在控制器中将Credenciado的对象作为$model传递,然后调用查询方法(如$model-> getValoresCredenciadosByConvenioRank())来获取数据提供程序。

<?php $this->widget('zii.widgets.grid.CGridView', array(
                'id' => 'credenciado-grid',
                // call your query function to get data provider (this is the name of function that you have implemented in your model)
                'dataProvider' => $model->getValoresCredenciadosByConvenioRank(),
                'filter' => $model,
                'afterAjaxUpdate' => "loadAfterAjax",
                'rowHtmlOptionsExpression' => 'array("style"=>"cursor: pointer","data-row-id" => $data->cre_id, "class" => "row_grid")',
                'columns' => array(
                        'cre_razao_social',
                    array(
                        'header'=>'Status',
                        'name'=>'cre_ativo',
                        'value'=>'Status::getStatus($data["cre_ativo"])',
                        'filter'=>array(true => 'ATIVO',false => 'INATIVO'),
                        'filterHtmlOptions'=>array('class'=>'col-sm-2'),
                    ),
                ),
     ));
?>

你的控制器会变成这样

$model = new Credenciado('getValoresCredenciadosByConvenioRank');
    $model->unsetAttributes();
    if (isset($_GET['Credenciado']))
        $model->setAttributes($_GET['Credenciado']);

    // pe($model);
    $modelEst = new Estabelecimento('search_adm');
    // pe($model);

    $modelEst->unsetAttributes();
    if (isset($_GET['Estabelecimento']))
        $modelEst->setAttributes($_GET['Estabelecimento']);

    $this->render('listar',array(
        'model' => $model,
        'modelEst' => $modelEst,
        'mes1' => $mes1,
        'mes2' => $mes2,
        'meses' => $meses,
        'produto' => $produto,
        'data' => $format,
        'datas' => $date_unique,
        'series' => $series,
        'ano1' => $ano1,
        'ano2' => $ano2
    ));
}

您模型将返回CArrayDataProvider而不是这样的查询结果,

$queryResult = $q->queryAll();
// return data provider for view instead of query result
return new CArrayDataProvider($queryResult, array());

希望这个解决方案能解决你的问题。让我知道它是否有效或者问题是否仍然存在。

相关问题