I
I
Igor Fedyaev2018-12-21 22:52:01
Yii
Igor Fedyaev, 2018-12-21 22:52:01

Create checkboxlist, write or update, value 1 and 0 in db?

Hello. I can't solve the problem with checkboxlist. the display is ok.
view:

<?php $form = ActiveForm::begin([
        'id' => 'form-input-example',
        ],
    ]) ?>
    <?= $form->field($model, 'status_id')
        ->checkboxList(ArrayHelper::map(Blog::find()->all(), 'id', 'status_id')) ?>
    <?= Html::submitButton('Сохранить', ['class' => 'btn btn-primary']) ?>
    <?php ActiveForm::end(); ?>

BlogController:
public function actionTests()
    {
        $model = new Blog();

        $model->status_id = Blog::find()->where('status_id')->all();

        if ($model->load(Yii::$app->request->post()) && $model->save())
        {
// Сделать что-то после записи, например SetFlash

        }

        return $this->render('tests', [
            'model' => $model,
        ]);
    }

Blog(Model):
public function rules()
    {
        return [
            ['status_id', 'safe'],
        ];
    }

5c1d430cf13e9297392047.jpegTest DB
5c1e1b2f8756c803708349.jpeg
How to write or update an existing value? Through set and get?
I read the documentation, but I still can't understand how the checkboxlist is implemented.
Help me implement this functionality))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question