Migrasi dataProvider Yii1 ke Yii2

Bingung mau judul apa om he, pokonya sejak pake Yii1 saya jarang pake bawaan Yii maksudnya bawaan Gii nya hehe, pengen hasilnya dinamis aja bisanya pake DAO jadi bisa kita modify sendiri malah migrasi ke Yii1 jadi tambah mudah banget, misalkan dataprovider yg result biasa kita tampilkan datanya  di Cgridview (Yii1) or Gridview (Yii2), langsung aja deh, kita pertama

Script Yii1


$key="nip";
$sql1="SELECT COUNT(*) FROM pnsmain ";

$sql="SELECT nip,nama,kdgol,status,jnskel FROM pnsmain ";

$count=Yii::app()->db->createCommand($sql1)->queryScalar();
$dataProvider=new CSqlDataProvider($sql, array(
'keyField' => $key,
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'nip', 'nama','kdgol','status','jnskel'
),
),
'pagination'=>array(
'pageSize'=>100,
),
));
return $dataProvider;

script Yii2, sederahanya aja setiap sintax $var= array( … ) yg ada di Yii1 jadi $var = [ … ]  di Yii2, dan juga app() di Yii1 jadi $app di Yii2, so begini


$sql1="SELECT COUNT(*) FROM pnsmain ";

$sql="SELECT nip,nama,kdgol,status,jnskel FROM pnsmain ";

$count= \Yii::$app->db->createCommand($sql1)->queryScalar();
$dataProvider=new CSqlDataProvider(['sql'=>$sql,
'totalCount'=>$count,
'sort'=>[
'attributes'=>[
'nip', 'nama','kdgol','status','jnskel'
],
],
'pagination'=>[
'pageSize'=>100,
],
]);

return $dataProvider;

moga bermanfaat🙂 maaf nubi lagi belajar

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: