yii2文件上传中非对象调用成员函数saveAs()

Call to a member function saveAs() on a non-object in yii2 file upload

我正在使用 kartik fileUpload 扩展在 yii2 中上传文件,一旦我 select 一个文件并提交,我得到

Call to a member function saveAs() on a non-object

我已经检查了其他 post 关于这个问题,但它没有帮助,

我的查看代码..

 <?php  echo $form->field($documents, 'sars_certificate')->label(false)->
  widget(FileInput::classname(), [
    'pluginOptions' => [
        'showCaption' => true,'showRemove' => true,'showClose' => true,
        'showPreview' => true,'uploadAsync' => true,
        'showUpload' => false,'maxFileSize'=> 2000,'autoReplace'=> true, 
        'placeholder' => 'Select a File...',],]); ?>

我的型号代码..

 [['sars_certificate'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,pdf,jpeg']

还有我的控制器代码..

  public function actionCreate()
  {   
  $model = new Projects(); 
  $documents = new ProjectDocuments();    

$borrower_id = Yii::$app->user->identity->id;
  $code = $model->accessCode(10);
  if ($model->load(Yii::$app->request->post())) 
  {
   //All $model related things will bw done

  //In this im doing file upload
  $documents->borrower_id = $borrower_id; 
  $documents->project_id = $code;    
  $documents->save(false);  

  $documents->sars_certificate = UploadedFile::getInstances($documents,'sars_certificate');

  $documents->sars_certificate->saveAs('user/purchase_order/' . $documents->sars_certificate->baseName . '.' . $documents->sars_certificate->extension);
  $documents->sars_certificate = $documents->sars_certificate;
  $documents->save(false); 

         return $this->redirect(['index']);
    } 

我给了if($documents->validate()){ **** }。但它本身不符合 if 条件,所以我删除了它。现在它说上面的错误..

请任何人帮忙,我在这上面浪费了很多时间...

1) 为避免重复图像,请在图像名称中附加 time()

2) 删除 UploadInstance.

之前的 $documents->save(false);
public function actionCreate() {
  $model = new Projects();
  $documents = new ProjectDocuments();

  $borrower_id = Yii::$app->user->identity->id;
  $code = $model->accessCode(10);
  if ($model->load(Yii::$app->request->post())) {
    //All $model related things will bw done
    //In this im doing file upload
    $documents->borrower_id = $borrower_id;
    $documents->project_id = $code;


    if(UploadedFile::getInstance($documents, 'sars_certificate')){
      $image = UploadedFile::getInstance($documents, 'sars_certificate'); 
      $imageName = time().$image->name;
      $path = "user/purchase_order/".$imageName; 
      if($image->saveAs($path)){
       $documents->sars_certificate = $imageName;
      }
    }

    $documents->save(false);
    return $this->redirect(['index']);
  }
}

更新

public function actionCreate() {
  $model = new Projects();
  $documents = new ProjectDocuments();

  $borrower_id = Yii::$app->user->identity->id;
  $code = $model->accessCode(10);
  if ($model->load(Yii::$app->request->post())) {
    //All $model related things will bw done
    //In this im doing file upload
    $documents->borrower_id = $borrower_id;
    $documents->project_id = $code;


    if(UploadedFile::getInstance($documents, 'sars_certificate')){
      $image = UploadedFile::getInstance($documents, 'sars_certificate');
      if($image){
        $imageName = time().$image->name;
        $path = "user/purchase_order/".$imageName; 
        if($image->saveAs($path)){
         $documents->sars_certificate = $imageName;
        }
      }
    }

    $documents->save(false);
    return $this->redirect(['index']);
  }
}