While selecting update, i want to populate the data to the checkboxlist from a database and show the boxes ticked for the selected Vendor to Update. I have added the screenshot of my update page.
5441
In my VendorCuisineController, code for update is as follows:
public function actionUpdate($VendorId,$CuisineId)
{
$record = $this->findCuisines($VendorId);
$model = new VendorCuisine;
$model1 = $this->findModel($VendorId, $CuisineId);
$i=0;
if($model->load(Yii::$app->request->post()))
{
foreach ($record as $items)
{
$database[$i]=$items->CuisineId;
$i++;
}
$length = count($model->CuisineId);
for ($i=0;$i<$length;$i++) {
$postbase[$i]=$model->CuisineId[$i];
}
$result = array_diff($postbase,$database);
foreach ($result as $item) {
$vc = new VendorCuisine();
$vc->VendorId = $VendorId;
$vc->CuisineId = $item;
$vc->save();
}
$result = array_diff($database,$postbase);
foreach ($result as $item) {
$this->findModel($VendorId, $item)->delete();
}
return $this->redirect(['index']);
}
else {
$model2=['VendorId'=>$VendorId,
'CuisineId'=>$this->findCuisineIdOnly($VendorId),
];
return $this->render('update', [
'model' => $model1,
]);
}
}
and the code in _form is as follows:
<div class="vendor-cuisine-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'VendorId')->textInput() ?>
<?= $form->field($model, 'CuisineId')->checkboxList($model->DataList);?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>