M
M
Michael2019-01-08 19:28:38
Yii
Michael, 2019-01-08 19:28:38

Error "Call to a member function saveAs() on a non-object" what is the error?

Hello everybody! Please tell me what is causing the error "Call to a member function saveAs() on a non-object", this error indicates that, in my case, the error shows that when saving the form, the document is not attached, with the documents attached, saving is normal .
5c34cf1339f9b489470907.png
Controller

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

    if ($model->load(Yii::$app->request->post())){
        $model->alias = str2url($res . ' ' . $model->name); 
        $model->docFile = UploadedFile::getInstance($model, 'docFile'); // Загрузка документа
        if ($model->validate()) {
            if ($model->save(false)) {
                $model->gallery = UploadedFile::getInstances($model, 'gallery');
                $model->uploadGallery();
            }
            if($model->path = $model->upload()) { // Присваиваем путь возвращаемый upload()
                if ($model->save(false)) {
                    // return $this->redirect(['view', 'id' => $model->id]);
                }
            }
            Yii::$app->session->setFlash('success', "Материал {$model->name} добавлен");
            return $this->redirect(['view', 'id' => $model->id]);
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);
}

Model
public function rules()
{
    return [
        [['docFile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'txt, pdf, djvu, doc, docx, xls, xlsx, ppt, pptx, zip, rar, 7z, tiff'],
    ];
}

public function upload()
{    
    if ($this->validate()) {
        $path = $this->processMkDir();
        $fileName = Inflector::slug(pathinfo($this->str2url($this->docFile->baseName), PATHINFO_FILENAME)) . '.' . $this->docFile->extension;
        $this->docFile->saveAs($path . $fileName);
        return $fileName;
    } else {
        return false;
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
G
grinat, 2019-01-08
@Bally

Instance came null https://github.com/yiisoft/yii2/blob/master/framew...
Check it for null

M
Michael, 2019-01-08
@Bally

grinat , Could you point out or show on my example how to implement the check, as I understand it, the check should be done in the upload function in the model, but I don’t understand how to write it correctly, since I tried, I only got new errors related with a misspelled function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question