R
R
Ruslan Tilyaev2019-04-30 13:07:50
Yii
Ruslan Tilyaev, 2019-04-30 13:07:50

Why is the path to the image not being loaded into the yii2 table?

There are two fields (passport and diploma), the user will send his passport and diploma to the site, respectively, the pictures are saved on the server and the path to the pictures is saved in the database table. I tried to implement this, as a result, the pictures are saved to the server, and the path is not written to the table.
The form:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'passport')->fileinput()->label(false) ?>
<?= $form->field($model, 'diploma')->fileinput()->label(false) ?>
<?= Html::submitButton('Отправить') ?>
<?php ActiveForm::end() ?>

Model:
public function rules()
    {
        return [  
            [['passport', 'diploma'], 'file', 'extensions' => 'png, jpg'],
        ];
    }

Controller:
public function actionCreate()
    {
        $model = new Clients();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $model->passport = UploadedFile::getInstance($model, 'passport');
                $model->diploma = UploadedFile::getInstance($model, 'diploma');
                $path = 'img/store/' . $model->id_cart . '/';
                FileHelper::createDirectory($path);
                if ($model->passport) {
                    $file = $path . $model->passport->baseName . '.' . $model->passport->extension;
                    $model->passport->saveAs($file);
                }
                if ($model->diploma) {
                    $file = $path . $model->diploma->baseName . '.' . $model->diploma->extension;
                    $model->diploma->saveAs($file);
                }

                Yii::$app->session->setFlash('success', 'Спасибо, мы получили вашу анкету. В скором времени мы с вами свяжемся. Ваш ID Анкеты - '
                    . $model->id_cart
                    . ". Вы можете проверит статус своей анкеты в разделе - <a href='/card'>Моя анкета</a> ");
            } else {
                Yii::$app->session->setFlash('error', 'Ошибка... Попробуйте еще раз...');
            }
            return $this->refresh();
        }

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

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Moses Fender, 2019-04-30
@mosesfender

Probably because $model->passport and $model->diploma are assigned after $model->save(), and then $model->save() is gone.

D
Decadal, 2019-04-30
@Decadal

Your model is weird. It stores file instances, not file paths. Firstly, before saving the model, it is worth checking whether everything is fine with the files, and secondly, lines with paths to images must be explicitly written into the model fields. Yii will not make a string out of a file object.
By the way, it would be better to break it into two models - in fact, the model for files, which is inherited from the yii base model. And a model for clients that inherits from active record and contains diploma_img_path and passport_img_path fields

R
Ruslan Tilyaev, 2019-04-30
@Heckfy325

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

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $model->passport = UploadedFile::getInstance($model, 'passport');
                $model->diploma = UploadedFile::getInstance($model, 'diploma');
                $path = 'img/store/' . $model->id_cart . '/';
                FileHelper::createDirectory($path);
                if ($model->passport) {
                    $model->save(false);
                    $file = $path . $model->passport->baseName . '.' . $model->passport->extension;
                    $model->passport->saveAs($file);
                }
                if ($model->diploma) {
                    $model->save(false);
                    $file = $path . $model->diploma->baseName . '.' . $model->diploma->extension;
                    $model->diploma->saveAs($file);
                }

                Yii::$app->session->setFlash('success', 'Спасибо, мы получили вашу анкету. В скором времени мы с вами свяжемся. Ваш ID Анкеты - '
                    . $model->id_cart
                    . ". Вы можете проверит статус своей анкеты в разделе - <a href='/card'>Моя анкета</a> ");
            } else {
                Yii::$app->session->setFlash('error', 'Ошибка... Попробуйте еще раз...');
            }
            return $this->refresh();
        }

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question