dataprovider sort problem

I am trying to use the sort element of the dataprovider at a gridview but it does not work,what maybe wrong?

I use the bellow vode at controller


$dataProvider = new \yii\data\SqlDataProvider([

            'sql' => 'select id,email,name,username,role from mechanics ;',

            'totalCount' => $count,

            'key' => 'id',

            'sort' => [

                //'defaultOrder' => ['name' => SORT_DESC,],

                'attributes' => ['email','name','username','role']

                    

                ],

            'pagination' => [

                'pageSize' => 50,

            ]

        ]);

at view


<?=

yii\grid\GridView::widget([

    'dataProvider' => $dataProvider,

    'columns' => [


        'email',

         'username',

        'name',

        ['attribute' => 'role',

            'value' => function($model) {

                       $usertype=new \app\models\Mechanics();

                       $type=$usertype->Usertype();

                        return $type[$model['role']];

                    }

            

        ],

        [

            'class' => 'yii\grid\ActionColumn',

            // you may configure additional properties here

            'buttons' => [.....

Hi dimis283,

Try removing the semicolon at the end of the sql.


$dataProvider = new \yii\data\SqlDataProvider([

            // 'sql' => 'select id,email,name,username,role from mechanics ;',

            'sql' => 'select id,email,name,username,role from mechanics',



SqlDataProvider will try to append ‘ORDER BY’ clause to the sql according to the sorting instructions. The semicolon at the end of the sql will be an obstacle.




'select id,email,name,username,role from mechanics ; order by email asc',