S
S
slip312019-04-13 19:51:28
Yii
slip31, 2019-04-13 19:51:28

How to save the file name in the database?

Something fell into a stupor.
Need to store the name of the saved file in the base field 'photo_url'
Controller

public function actionCreate() {
        $model = new Doctors();
        if ($model->load(Yii::$app->request->post())) {
            $model->image = \yii\web\UploadedFile::getInstance($model, 'image');          
            if ($model->save()) {
                  $model->uploadPhoto();
                $model->photo_url = $model->fileName;               
                $this->saveSpecialities($model);               
            }
            return $this->redirect(['view', 'id' => $model->id]);
        }
        return $this->render('create', [
                    'model' => $model
        ]);
    }

 protected function saveSpecialities($model) {
        foreach ($model->specialites as $var) {
            $speciality = new DoctorsSpeciality();
            $speciality->speciality_id = $var->id;
            $speciality->doctor_id = $model->id;
            $speciality->save();
        }
    }

And in the model
public function uploadPhoto() {
        if ($this->validate()) {
            $this->fileName = $this->generateSlug() . '.' . $this->image->extension;   //Название файла генерю из названия         
            $this->image->saveAs($this->path . $this->fileName);
            Image::thumbnail($this->path . $this->fileName, 200, 200)->save($this->path_middle . $this->fileName, ['quality' => 100]);
            Image::thumbnail($this->path . $this->fileName, 100, 100)->save($this->path_small . $this->fileName, ['quality' => 100]);
            return true;
        } else {
            return false;
        }
    }

files are saved, everything is fine, but I need to write the name to the $fileName database.
But I get as a result
SQLSTATE[HY000]: General error: 1364 Field 'photo_url' doesn't have a default value

Apparently I don't get $fileName into the controller.
What did I do wrong?

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