paginationyii2csqldataprovider

yii2: SqlDataProvider with pagination and searcModel getting error preg_match() expects parameter 2 to be string, object given


i want to show data using sqlDataProvider to gridview from different table and calculate it, here my code in siteController.php
public function actionSyahriyah() { $searchModel = new SyahriyahSearch(); $db = Yii::$app->db; $bayar = $db ->createCommand('SELECT sy.no_syahriyah, sy.banyak, sa.nama, sy.tgl, sa.tarif FROM santri sa, syahriyah sy WHERE sa.no_induk = sy.no_induk ORDER BY sy.tgl'); $dataProvider = new SqlDataProvider([ 'sql' => $bayar, 'pagination' => [ 'pageSize' => 5 ], ]); return $this->render('syahriyah',[ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); }
and this is the gridview:
<?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ // ['class' => 'yii\grid\SerialColumn'], 'tgl', 'banyak', ], 'export' => false, ]); ?>


Solution

  • You can use dataProvider

    <?php
    
        $bayar = $db ->createCommand('SELECT sy.no_syahriyah, sy.banyak, sa.nama, sy.tgl, sa.tarif 
               FROM santri sa, syahriyah sy 
               WHERE sa.no_induk = sy.no_induk
               ORDER BY sy.tgl');
    
        $dataProvider = new SqlDataProvider([
            'sql' => $bayar,
            'pagination' => [
                'pageSize' => 5 
                ],
            ],
        ]);
    
    ?>