N
N
nnkrasovok2019-05-19 19:20:42
Yii
nnkrasovok, 2019-05-19 19:20:42

Why is the file name not being written to the database?

Implemented adding a file. The file is saved in the project folder, but the name is not transferred to the database. I can’t understand why
the UploadCVForm model, where the file is saved to a folder and the file name is returned

public $file;


    public function upload(UploadedFile $file)
    {
        $this->file = $file;
        $filename = strtolower(md5(uniqid($file->baseName)). '.' .$file->extension);
        $file->saveAs('uploads/' . $filename);
        return $filename;
    }

controller
$model_file = new UploadCVForm();

        if (\Yii::$app->request->isPost) {
            $profile = $this->module->manager->findProfileById(\Yii::$app->user->identity->getId());
            $file = UploadedFile::getInstance($model_file, 'file');
            $profile->saveFile($model_file->upload($file));
            if ($model_file->upload($file)) {
                // file is uploaded successfully
                return $this->refresh();
            }
        }

the saveFile method of the model, where, in theory, the file name should be saved in the field (CV) of the table
public function saveFile($filename)
    {
        $this->CV = $filename;
        return $this->save(false);
    }

and view
<?php $form = \yii\widgets\ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

                <?= $form->field($model_file, 'file')->fileInput() ?>

                <?= \yii\helpers\Html::submitButton(Yii::t('user', 'Добавить'), ['class' => 'btn btn-success']) ?><br>

                <?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