N
N
nnkrasovok2019-05-17 17:23:26
Yii
nnkrasovok, 2019-05-17 17:23:26

How to change a database entry?

I'm trying to change a database entry. The problem is that when I click the "change" button for a specific record, then in the change view of this record (update_competence): the id of the record is passed correctly, the experience field is passed correctly, but the "id_subcategory" field is incorrectly passed (displays a completely different field name, what's in the database), as well as when changing - nothing saves the
Controller

public function actionUpdate_competence($id)
    {
        $model = $this->findModelCompetence($id);

        $subcategory1 = Category::find()->where(["id" => 1])->one()->getSubcategory()->asArray()->all();
        $category = Category::find()->asArray()->all();
        $subcategory2 = Category::find()->where(["id" => 2])->one()->getSubcategory()->asArray()->all();
        $subcategory3 = Category::find()->where(["id" => 3])->one()->getSubcategory()->asArray()->all();
        $subcategory4 = Category::find()->where(["id" => 4])->one()->getSubcategory()->asArray()->all();
        $subcategory5 = Category::find()->where(["id" => 5])->one()->getSubcategory()->asArray()->all();
        $subcategory6 = Category::find()->where(["id" => 6])->one()->getSubcategory()->asArray()->all();
        $subcategory7 = Category::find()->where(["id" => 7])->one()->getSubcategory()->asArray()->all();
        $subcategory8 = Category::find()->where(["id" => 8])->one()->getSubcategory()->asArray()->all();

        if ($model->load(\Yii::$app->request->post()) && $model->save()) {
            \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Компетенция успешно изменена'));
            return $this->redirect(['competence']);
        }

        return $this->render('update_competence', [
            'model' => $model, 'subcategory1' => $subcategory1,
            'subcategory2' => $subcategory2,
            'subcategory3' => $subcategory3,
            'subcategory4' => $subcategory4,
            'subcategory5' => $subcategory5,
            'subcategory6' => $subcategory6,
            'subcategory7' => $subcategory7,
            'subcategory8' => $subcategory8,
            'category' => $category
        ]);
    }

    /**
     * Finds the ConsSubcategoryForm model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ConsSubcategory the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModelCompetence($id)
    {
        if (($model = ConsSubcategory::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

View update_competence
<div class="category-update" style="margin-top: 100px;">

    <h1><?= Html::encode($this->title) ?></h1>

    <?php $form = \yii\widgets\ActiveForm::begin([
        'id' => 'competence-form',
        'options' => ['class' => 'form-horizontal'],
        'fieldConfig' => [
            'template' => "{label}\n<div class=\"col-lg-9\">{input}</div>\n<div class=\"col-sm-offset-3 col-lg-9\">{error}\n{hint}</div>",
            'labelOptions' => ['class' => 'col-lg-3 control-label'],
        ],
        'enableClientValidation' => true
    ]); ?>

    <?= $form->field($model, 'experience') ?>


    <?= $form->field($model, 'id_subcategory')->dropDownList(
        [
            $category[0]['name'] => array_column($subcategory1, 'name'),
            $category[1]['name'] => array_column($subcategory2, 'name'),
            $category[2]['name'] => array_column($subcategory3, 'name'),
            $category[3]['name'] => array_column($subcategory4, 'name'),
            $category[4]['name'] => array_column($subcategory5, 'name'),
            $category[5]['name'] => array_column($subcategory6, 'name'),
            $category[6]['name'] => array_column($subcategory7, 'name'),
            $category[7]['name'] => array_column($subcategory8, 'name')
        ],
        [
            'prompt' => 'Выбрать категорию'
        ]
    ) ?>

    <div class="form-group">
        <div class="col-lg-offset-3 col-lg-9">
            <?= \yii\helpers\Html::submitButton(Yii::t('user', 'Сохранить'), ['class' => 'btn btn-success']) ?><br>
        </div>
    </div>

    <?php \yii\widgets\ActiveForm::end(); ?>

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