Save value of checkboxlist to db in yii2

Viewed 1434

I have a yii2 form which contain a checkbox list items which i made like this:

<?php $CheckList = ["users" => 'Users', "attendance" => 'Attendance', "leave" => 'Leave', "payroll" => 'Payroll'];?>

    <?= $form->field($model, 'MenuID')->checkboxList($CheckList,['separator'=>'<br/>']) ?>

Now what i need is to save the values in the database column as a comma separated value.

I tried to modify the create function in my controller in this way:

 public function actionCreate()
{
    $model = new Role();

    if ($model->load(Yii::$app->request->post())) {


        if ($model->MenuID != " ") {
           $model->MenuID = implode(",", $model->MenuID);                
        }
         $model->save();             
        return $this->redirect(['view', 'id' => $model->RoleID]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

But the values are not being saved in the database

1 Answers
Related