7

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

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

At the following Url: http://localhost/advanced/frontend/web/index.php?r=vrisorse%2Findex

I have the error:

Not Supported – yii\base\NotSupportedException Message format 'number' is only supported for integer values. You have to install PHP intl extension to use this feature. 1. in C:\xampp\htdocs\advanced\vendor\yiisoft\yii2\i18n\MessageFormatter.php

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

'COD_RISORSA','NOME', 'COGNOME' are columns of the select.

2
  • The error seems related to intl feature, post your config please. Commented Apr 29, 2015 at 15:47
  • in your php.ini uncomment extension=php_intl.dll and restart webserver Commented Apr 30, 2015 at 5:52

5 Answers 5

4

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

and below is my view

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

           'Booking_id',
           'Booking_date',
           'Movie',
           'Theatre',
           'Amount',
           //~ ['class' => 'yii\grid\ActionColumn'],
       ],
   ]); ?>
Sign up to request clarification or add additional context in comments.

Comments

2

I met the same problem,It is easy to solve.You need assign the DB as bellow:

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

Comments

1

There documentation: https://www.yiiframework.com/doc/guide/2.0/en/output-data-providers#sql-data-provider

Example:

Create $dataProvider (controller):

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

Create GridView (view):

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

Comments

0

You need to uncomment

extension=php_mysql.dll

and

extension=php_mysqli.dll

in your php.ini, let me know if works

Comments

0

The simplest solution to the basic question is:

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

Flesh it out as complex as you like using SqlDataProvider and GridView params.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.