yii 在gridview中自定义sql查询

czq61nw1  于 2022-11-09  发布在  其他
关注(0)|答案(5)|浏览(171)

我对Yii 2比较陌生。我用的是高级结构
我需要在不使用模型的情况下在视图中显示自定义sql结果,因为我想显示sql视图。
index.php

<?= GridView::widget([
       'dataProvider' => $dataProvider,
       'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'COD_RISORSA',
            [
                'label' =>"Nome",
                'attribute' => 'NOME',
                'value'=>function($data){
                    return $data["NOME"];
                }
            ],
            'COGNOME',
            ['class' => 'yii\grid\ActionColumn'],
        ],
   ]); ?>

VRisorseController.php

public function actionIndex()
{

    $totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM v_risorse')->queryScalar();

    $dataProvider = new SqlDataProvider([
        'sql' => 'SELECT * FROM v_risorse',
        'totalCount' => $totalCount,
        'sort' =>false,
        'pagination' => [
            'pageSize' => 10,
        ],
    ]);

    return $this->render('index', [
        'dataProvider' => $dataProvider,
    ]);
}

位于以下URL:http://localhost/advanced/frontend/web/index.php?r=vrisorse%2Findex
我有错误:
不支持- yii\base\NotSupportedException消息格式'number'只支持整数值。您必须安装PHP intl扩展才能使用此功能。1.在C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\i18n\MessageFormatter.php中
我试图注解gridview中的所有列,错误似乎与$dataProvider变量有关
'COD_RISORSA','NOME', 'COGNOME'是选择的列。

rt4zxlrg

rt4zxlrg1#

你需要安装PHP intl扩展。我有同样的错误
下面给出的是我在控制器中的工作代码

$count      =   Yii::$app->db->createCommand('
                    SELECT COUNT(*) FROM screen_ticket_booking_history WHERE status=:status
                    ', [':status' => 0])->queryScalar();

                    $sql =  "SELECT A1.booking_id As Booking_id,
                                A1.booking_date As Booking_date,
                                A2.movie_name As Movie,
                                A3.theatre_name As Theatre,
                                A1.amount As Amount

                                FROM 
                                screen_ticket_booking_history A1

                                LEFT OUTER JOIN movies A2 ON A1.movie_id=A2.id
                                LEFT OUTER JOIN theatres A3 ON A1.theatre_id=A3.id
                                LEFT OUTER JOIN users_backend A4 ON A3.users_backend_id=A4.id

                                WHERE A1.booking_date = '{$day}'
                                AND   A1.movie_id='{$movies->id}'";

                    //~ $models = $dataProvider->getModels(); //print_r($models);die();
                    if( $userid != '1')
                    { 
                        $sql .= " AND A3.users_backend_id = '{$userid}' ";
                    }

                    $dataProvider = new SqlDataProvider([
                    'sql' => $sql,
                    'totalCount' => $count,
                    ]);
                    return $this->render('index',
                    [   'model'             => $model,
                        'dataProvider'      => $dataProvider,
                    ]);
                }

下面是我的观点

<?= GridView::widget([
       'dataProvider' => $dataProvider,
       'columns' => [
           ['class' => 'yii\grid\SerialColumn'],

           'Booking_id',
           'Booking_date',
           'Movie',
           'Theatre',
           'Amount',
           //~ ['class' => 'yii\grid\ActionColumn'],
       ],
   ]); ?>
ldioqlga

ldioqlga2#

我遇到了同样的问题,这很容易解决。你需要如下分配DB:

public function actionIndex()
{

    $totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM v_risorse')->queryScalar();

    $dataProvider = new SqlDataProvider([
      **'db' => Yii::$app->db,**
        'sql' => 'SELECT * FROM v_risorse',
        'totalCount' => $totalCount,
        'sort' =>false,
        'pagination' => [
            'pageSize' => 10,
        ],
    ]);

    return $this->render('index', [
        'dataProvider' => $dataProvider,
    ]);
}
xiozqbni

xiozqbni3#

文件:https://www.yiiframework.com/doc/guide/2.0/en/output-data-providers#sql-data-provider
示例:

创建$dataProvider(控制器):

public function actionIndex($src, $disposition, $start, $finish)
    {
                $count = $connection->createCommand("
                select COUNT(*)
                from cdr 
                where (Length(dst) > 6) 
                and (start between :start and :finish)  
                and (lastapp=\"Dial\") 
                and src in (:src)
                and disposition in ('ANSWERED', 'FAILED', 'BUSY', 'NO ANSWER')")
                ->bindValue(':src', $params['src'])
                ->bindValue(':start', $params['start'])
                ->bindValue(':finish', $params['finish'])->queryScalar();

                $sql = "select src, dst,
                Case Disposition
                when 'ANSWERED' then 'Отвечено'
                when 'FAILED' then 'Ошибка'
                when 'BUSY' then 'Занято'
                when 'NO ANSWER' then 'Не дозвонились'    
                else 'ИТОГ'    
                End as status,
                Start,
                Round(billsec/60, 2) as Billing,
                uniqueid
                from cdr 
                where (Length(dst) > 6) 
                and (start between :start and :finish)  
                and (lastapp=\"Dial\") 
                and src in (:src)
                and disposition in ('ANSWERED', 'FAILED', 'BUSY', 'NO ANSWER')";

                $boundParams = [
                   ':src' => $params['src'],
                   ':start' => $params['start'],
                   ':finish' => $params['finish'],
                ];

        //   Provider
        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'params' => $boundParams,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => 150,
            ],
            'sort' => [
                'attributes' => [
                    'src',
                    'dst',
                    'Start',
                    'Billing',
                    'uniqueid',
                ],
            ],
        ]);
        return $this->render('index', [
            'params' => $params,
            'dataProvider' => $dataProvider,
            'count' => $count,
        ]);
    }
}

创建网格视图(视图):

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'label' => 'От кого',
            'attribute' => 'src',
//            'format' => ['decimal', 2],
        ],
        [
            'label' => 'Кому',
            'attribute' => 'dst',
//            'format' => ['decimal', 2],
        ],
        [
            'label' => 'Начало',
            'attribute' => 'Start',
//            'format' => ['decimal', 2],
        ],
        [
            'label' => 'Длительность (сек)',
            'attribute' => 'Billing',
//            'format' => ['decimal', 2],
        ],
        [
            'label' => 'UUID',
            'attribute' => 'uniqueid',
//            'format' => ['decimal', 2],
        ],
        //~ ['class' => 'yii\grid\ActionColumn'],
    ],
]);
icomxhvb

icomxhvb4#

您需要取消注解
扩展名= php_mysql. dll

扩展名= php_mysqli. dll
在你的php.ini里,如果可以的话,让我知道

dzhpxtsq

dzhpxtsq5#

对这个基本问题最简单的解决方法是:

$dataProvider = new SqlDataProvider([
            'sql' => '<your query/>',
    ]);
    echo GridView::widget([
            'dataProvider' => $dataProvider,
    ]);

使用SqlDataProvider和GridView参数将它变得尽可能复杂。

相关问题