gridview中的Yii2自定义sql查询

2022-01-04 00:00:00 sql php yii2 yii gridview

我对 Yii2 很陌生.我正在使用高级结构

I'm quite new to Yii2. I'm using advanced structure

我需要在不使用模型的情况下在视图中显示自定义 sql 结果,因为我想显示一个 sql 视图.

I need to show a custom sql result in a view without using a model because I would like to display a sql view.

index.php

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

VRisorseController.php

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,
    ]);
}

在以下网址:http://localhost/advanced/frontend/web/index.php?r=vrisorse%2Findex

我有错误:

不支持 – yiiaseNotSupportedException 消息格式 'number'仅支持整数值.你必须安装 PHP intl扩展以使用此功能.1.在C:xampphtdocsadvancedvendoryiisoftyii2i18nMessageFormatter.php

Not Supported – yiiaseNotSupportedException Message format 'number' is only supported for integer values. You have to install PHP intl extension to use this feature. 1. in C:xampphtdocsadvancedvendoryiisoftyii2i18nMessageFormatter.php

我尝试注释gridview中的所有列,错误似乎与$dataProvider变量有关

I tried to comment all the columns in gridview, and the error seems to be related to $dataProvider variable

'COD_RISORSA','NOME', 'COGNOME' 是选择的列.

推荐答案

您需要安装 PHP intl 扩展.我有同样的错误

You need to install PHP intl extension.i had the same error

下面给出的是我的工作代码在控制器中

given below is my working code in controller

$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' => 'yiigridSerialColumn'],

           'Booking_id',
           'Booking_date',
           'Movie',
           'Theatre',
           'Amount',
           //~ ['class' => 'yiigridActionColumn'],
       ],
   ]); ?>

相关文章