Yii2: Call to a member function saveAs() on null w

2019-09-17 22:02发布

问题:

While uploading multiple file getting this error:

When I put [['file'], 'file', 'maxFiles' => 4],in model getting following error:

Call to a member function saveAs() on null

But when I put this [['file'], 'file'], in model, its uploading.

Why am I getting error?

View:

<?php  echo $form->field($model,'file[]')->label(false)->widget(FileInput::classname(), 
                          [
                          'options'=>['accept'=>'image/*', 'multiple'=>true],
                          'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
                          ]]);  
                      ?> 

Controller:

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

        if ($model->load(Yii::$app->request->post()))
        {
            $imageName = $model->room_type;
            $model->file = UploadedFile::getInstance($model, 'file');
            $model->file->saveAs( 'uploads/room_img/'.$imageName.'.'.$model->file->extension);
            //save the path in the db column
            $model->images = 'uploads/room_img/'.$imageName.'.'.$model->file->extension;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } 
        else 
        {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

回答1:

Use getInstances instead of getInstance as according to their respective documentations, the first returns all uploaded files for a given model attribute while the second is designed to return a single one. Then loop and save them one by one :

if ($model->load(Yii::$app->request->post())) { 
     $imageName = $model->room_type;

     $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');

     $all_files_paths = [];

     foreach ($model->imageFiles as $file_instance) {
        // this should hold the new path to which your file will be saved
        $path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;

        // saveAs() method will simply copy the file 
        // from its temporary folder (C:\xampp\tmp\php29C.tmp) 
        // to the new one ($path) then will delete the Temp File
        $file_instance->saveAs($path);

        // here the file should already exist where specified within $path and 
        // deleted from C:\xampp\tmp\ just save $path content somewhere or in case you need $model to be
        // saved first to have a valid Primary Key to maybe use it to assign
        // related models then just hold the $path content in an array or equivalent :
        $all_files_pathes []= $path;
     }

     $model->save();

     /*
        after $model is saved there should be a valid $model->id or $model->primaryKey
        you can do here more stuffs like :

        foreach($all_files_pathes as $path) {
            $image = new Image();
            $image->room_id = $model->id;
            $image->path = $path;
            $image->save();
        }
     */

     return $this->redirect(['view', 'id' => $model->id]);
 }

See docs for more info.



标签: php yii2