【问题标题】:Trying to get property of non-object while using kartik-v for image uploading in Yii2在Yii2中使用kartik-v进行图像上传时尝试获取非对象的属性
【发布时间】:2016-12-24 22:28:29
【问题描述】:

我正在使用yii2-widget-fileinput 在表单中上传图片。 当我点击上传或创建按钮时,控制器中出现Trying to get property of non-object 错误。

控制器

public function actionCreate()
    {
        Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/uploads/';
        $model = new Ads();
        $provinces = ArrayHelper::map(Province::find()->all(), 'name', 'name'); 
        if ($model->load(Yii::$app->request->post())){

            $image = UploadedFile::getInstances($model, 'image');
            $model->filename = $image->name;
            $ext = end((explode(".", $image->name)));
            $avatar = Yii::$app->security->generateRandomString().".{$ext}";
            $path = Yii::$app->params['uploadPath'].$avatar;
            if ($model->save()) {

                $image->saveAs($path);
                $model->image_adr = $path;
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                echo "error on saving the model";
            }

        }
        return $this->render('create', [
            'model' => $model,
            'provinces'=>$provinces,
            ]);
    }

示范规则

public function rules()
    {
        return [
            [['type', 'explanation', 'cost', 'province_name', 'address'], 'required'],
            [['type', 'explanation', 'image_adr', 'address'], 'string'],
            [['cost'], 'integer'],
            [['province_name'], 'string', 'max' => 20],
            [['province_name'], 'exist', 'skipOnError' => true, 'targetClass' => Province::className(), 'targetAttribute' => ['province_name' => 'name']],
            [['image'],'safe'],
            [['image'], 'file', 'extensions'=>'jpg, gif, png', 'maxFiles'=>3,],
        ];

最后是观点

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

问题应该是我认为的控制器中的这一部分

$image = UploadedFile::getInstances($model, 'image');  

错误的图像可能会有所帮助

【问题讨论】:

    标签: yii2 image-uploading


    【解决方案1】:

    您应该首先检查是否是帖子中的图像。

    ....
    
    $image = UploadedFile::getInstances($model, 'image'); //getInstanceByName
    
    if (!empty($image))
       $model->filename = $image->name;
    
      .....
      if ($model->save()) {
            if (!empty($image))
                    $image->saveAs($path);
    .........
    

    确保在您的表单中添加了 ency 类型:

    $form = ActiveForm::begin([ 
    'id' => 'form_id', 
    'options' => [ 
    'class' => 'form_class', 
    'enctype' => 'multipart/form-data', 
    ], 
    ]); 
    

    【讨论】:

    • 是的,正如错误所说,$image = UploadedFile::getInstances($model, 'image'); 没有返回你想要的,是的,图像不在帖子中。我该怎么办?
    • 您确定,您的表单中添加了 encytype 多部分数据吗?
    • 还是同样的问题,我已经用图片更新了问题。
    【解决方案2】:

    问题是当您使用UploadedFile::getInstances($model, 'image'); 时,您应该使用foreach 或将其视为数组。

    让我感到困扰的是,即使您使用的是 UploadedFile::getInstanc(请注意最后已过时的 s),您仍应将其视为数组,并且在所有部分中您都应该使用$image[0],而不是$iamge 孤独。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多