Answer the question
In order to leave comments, you need to log in
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();
}
}
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;
}
}
SQLSTATE[HY000]: General error: 1364 Field 'photo_url' doesn't have a default value
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question