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,
]);
?>
Asked
Active
Viewed 1,092 times
1

Adib Muhammad
- 15
- 6
1 Answers
0
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
],
],
]);
?>

ScaisEdge
- 131,976
- 10
- 91
- 107
-
i use your advice, but i got error _preg_match() expects parameter 2 to be string, object given_ in SqlDataProvider.php – Adib Muhammad Jun 25 '16 at 03:17
-
I'm sorry but you comment seems not related to the answer .. i don't see any preg_match .. eventually update you post and show the related code .. – ScaisEdge Jun 25 '16 at 09:39
-
Looking to your code You are using a search model and a dataProvider probalibly not related .. you should try without modelSearch .. and then you should create or exented you modelSearch with an search function the manage the relation you need .. otherwise you can't use the filter and search functionality.. for show, filter and search data from different table could be useful this link .. http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ – ScaisEdge Jun 26 '16 at 07:09