I
I
Ivan Yakushenko2016-07-17 13:36:39
Yii
Ivan Yakushenko, 2016-07-17 13:36:39

How to update data in Yii2 database?

There is a simple data entry form:

<div class="site-editdata">
    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'form-editdata']); ?>
            <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
            <?= $form->field($model, 'area1') ?>
            <?= $form->field($model, 'area2') ?>
            <?= $form->field($model, 'area3') ?>
            <?= $form->field($model, 'area4') ?>
            <?= $form->field($model, 'area5') ?>
            <?= $form->field($model, 'area6') ?>
            <?= $form->field($model, 'area7') ?>
            <?= $form->field($model, 'password')->passwordInput() ?>
            <div class="form-group">
                <?= Html::submitButton('Edit', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>

You need to update the corresponding cells in the table only if at least something is entered in the field. For example, if some data is entered in the username and area6 fields, then they need to be updated in the table, and the rest should be left as is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dave, 2016-07-17
@kshnkvn

You need to update the corresponding cells in the table only if at least something is entered in the field.

That is, it is necessary to execute the request only if those fields are filled in? Then at first put validation on these fields. That is, they should not be empty, and then the framework will not allow them to be updated if they are empty.
Starting from the 2nd version, only those fields (in the request) that have been changed are updated. If there were no changes at all, then there will be no queries to the database at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question