基本上,我想创建一个CRUD页面,它是来自Query类的dataProvider。
所以,在控制器中:
public function actionIndex(){
$searchModel = new RequestBisaApproveSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
然后在RequestBisaApproveSearch();
中,这是dataProvider:
public function search($params)
{
$karyawan = Karyawan::findOne(['id' => \Yii::$app->user->identity->karyawan_id]);
$dataKaryawan = $karyawan->linkKaryawanPerusahaanBranchDepartementJabatans;
$branches = [];
$perusahaans = [];
$departements = [];
$jabatans = [];
foreach ($dataKaryawan as $data) :
array_push($branches, $data['branch_id']);
array_push($perusahaans, $data['perusahaan_id']);
array_push($departements, $data['departement_id']);
array_push($jabatans, $data['jabatan_id']);
endforeach;
$query = (new Query())
->select("re.id as id, kar.first_name, kar.last_name, re.status, re.header, re.tanggal_permintaan")
->from('ytresnamuda_it.request re')
->join('LEFT JOIN', 'ytresnamuda_hrd.karyawan kar', 're.karyawan_id = kar.id')
->join('LEFT JOIN', 'ytresnamuda_hrd.link_karyawan_perusahaan_branch_departement_jabatan li', 'kar.id = li.karyawan_id');
$query->where(['IN','li.branch_id', $branches]);
$query->andWhere(['IN','li.perusahaan_id', $perusahaans]);
$query->andWhere(['IN','li.departement_id', $departements]);
$query->andWhere(['<','li.jabatan_id', max($jabatans)]);
$query->andWhere(['!=','li.karyawan_id', $karyawan->id ]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
问题是,何时制作actionColumn。 Yii2在actionColumn中有一个createUrl函数,如下所示:
[
'class' => 'kartik\grid\ActionColumn',
'dropdown' => false,
'width' => '150px',
'template' => '{approve} {print-request}',
'vAlign' => 'top',
'urlCreator' => function ($action, $model, $key, $index) {
return Url::to([$action, 'id' => $key]);
},
'buttons' => [
'approve' => function ($url, $model, $key) {
return Html::a( $key, Url::to(['request-bisa-approve/approve', 'id' => $key]),
[
'class' => 'btn btn-sm btn-primary btn-gii-action-customized fa fa-thumbs-o-up',
'role' => 'modal-remote',
'data-toogle' => 'tooltip',
'title' => 'Reminder IT'
]);
},
],
],
如果你能看到:
'urlCreator' => function ($action, $model, $key, $index) {
return Url::to([$action, 'id' => $key]);
},
id是$ key.So,我用另一列检查它,
[
'class' => '\kartik\grid\DataColumn',
'width' => '30px',
'attribute' => 'id',
'vAlign' => 'top',
],
结果是:id
是有效整数。但是$ key总是0。
很奇怪,但如果有人有线索,那就非常感激。