D
D
Dmitry2020-09-29 11:38:44
Yii
Dmitry, 2020-09-29 11:38:44

How to update different data in two tables from one form?

Good afternoon, I know that I already got everyone with questions, but please help me, how to update different data in two tables from one form?

There is a "features" table and a "features_value" table, on the site it looks like this where there are properties and its values:
5f72f1cce4577075011032.jpeg

and there is a form that should save both data in the "features" table and its values ​​in "features_value"!

The form itself:

<?php $form = ActiveForm::begin(); ?>

    <div class="row my-row-clear">
        <div class="col-lg-8 my-class-bg">
            <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
            <?= $form->field($model, 'url')->textInput(['maxlength' => true]) ?>
        </div>
        <div class="col-lg-3 col-lg-offset-1 my-class-bg">
            <?= $form->field($model, 'url_in_product')->dropDownList([0 => 'Нет', 1 => 'Да'], ['options' => ['0' => ['Selected' => true]]]) ?>
            <?= $form->field($model, 'in_filter')->dropDownList([0 => 'Нет', 1 => 'Да']) ?>
        </div>
    </div>

    <div class="value">
        <div class="value__title" style="display: flex;margin-bottom: 16px">
            <h4>Значения свойства</h4>
            <div class="value__btn" style="margin-left: 25px">
<!--                --><?//= Html::a('Добавить значение', ['create-value'], ['class' => 'btn btn-success', 'data-toggle' => 'modal', 'data-target' => '#w0']) ?>
<!--                --><?//= Html::a('Добавить значение', Url::to(['add-value', 'id' => $model->id]), ['class' => 'btn btn-success']) ?>
                <?= Html::a('Добавить значение', Url::to(['add-value', 'id' => $model->id]), ['class' => 'btn btn-success', 'data-toggle' => 'modal', 'data-target' => '#w0']) ?>
            </div>
        </div>
        <table class="table">
            <thead class="thead-dark" style="background-color: #222;color: #fff;">
            <tr>
                <th scope="col"></th>
                <th scope="col">Значение</th>
                <th scope="col">Кол-во товаров</th>
                <th scope="col">Индексировать</th>
                <th scope="col"></th>
                <th scope="col"></th>
            </tr>
            </thead>
            <tbody>
            <?php if($features):?>
            <?php foreach ($features as $fv): ?>
            <tr>
                <th scope="row"></th>
                <td>
                    <?= $form->field($fv, 'value')->textInput(['maxlength' => true])->label(false); ?>
                </td>
                <td>*</td>
                <td>
                    <?= $form->field($fv, 'to_index')->dropDownList([0 => 'Нет', 1 => 'Да'], ['options' => ['0' => ['Selected' => true]]])->label(false); ?>
                </td>
                <td></td>
                <td>
                    <?= Html::a('x', Url::to(['features/delvalue', 'id' => $fv->id]), ['data-method' => 'POST']) ?>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php else:?>
            <tr>
                <td></td>
                <td>
                    <span>Значения для данного свойства не добавлены</span>
                </td>
            </tr>
            <?php endif;?>
            </tbody>
        </table>
    </div>

    <div class="form-group">
        <?= Html::submitButton('Сохранить данные', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>


and a standard controller that only updates "features" and "features_value" values ​​are not updated, how to make them update too?

Controller itself:
public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $features = FeaturesValue::find()->where(['feature_id' => $id])->all();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->session->setFlash('success', "Настройки сохранены. Изменения отображаются на сайте!");
            return $this->redirect(['update', 'id' => $model->id]);
        }

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

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