yii2在相关模型中上传多张图片,如何更新操作?

时间:2019-02-12 15:27:54

标签: php yii2

actioncreate工作正常时 我去更新它时遇到错误。

这是actionCreate

的代码
/**
 * @return mixed
 */
public function actionCreate1()
{
    $model = new Biodata();
    $media = new BiodataMedia();

    if ($model->load(Yii::$app->request->post())) {
        $model->save();

        if ($media->validate()) {
            $images = uploadedFile::getInstances($media, 'image');
            foreach ($images as $image) {
                // $media = new BiodataMedia();
                $path = 'web/img/biodata/' . $image->basename . '.' . $image->extension;
                if ($image->saveAs($path)) {
                    $media = new BiodataMedia();
                    $media->image = $image->basename . '.' . $image->extension;
                    $media->image = '/' . $path;
                    $media->biodata_id = $model->id;
                    $media->save();
                }
            }

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

actionUpdate的代码如下:

/**
 * @param $id
 * @return mixed
 */
public function actionUpdate($id)
{
    $model = $this->findModel($id);

    $media = BiodataMedia::find()->where(['biodata_id' => $id])->asArray()->all();

    if ($model->load(Yii::$app->request->post())) {

        $model->save();
        if ($model->image == '') {
            $images = uploadedFile::getInstances($media, 'image');
            foreach ($images as $image) {
                // $media = new BiodataMedia();
                $path = 'web/img/biodata/' . $image->basename . '.' . $image->extension;
                if ($image->saveAs($path)) {
                    $media = new BiodataMedia();
                    $media->image = $image->basename . '.' . $image->extension;
                    $media->image = '/' . $path;
                    $media->biodata_id = $model->id;
                    $media->save();
                }
            }

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

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

相关的_form.php代码如下:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\jui\DatePicker;

/* @var $this yii\web\View */
/* @var $model app\models\Biodata */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="biodata-form">
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
    <div class="row">
    <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
     <?= $form->field($media, 'image[]')->fileInput(['multiple'=>true, 'accept'=>'image/*']) ?>       
    </div>
    <div class="form-group col-lg-12" style="text-align: center">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>
</div>

我得到的错误是: Call to a member function isAttributeRequired() on array

1 个答案:

答案 0 :(得分:0)

我认为这是一个很好的解决方法,任何人都需要为多图像上传操作寻找更新操作。 我所做的就像是使用删除操作在“上传和添加删除”按钮下方显示现有图像一样,因此,总而言之,您可以根据需要方便地删除图像并添加新图像。

_form.php中的代码

<?= $form->field($media, 'image[]')->fileInput(['multiple'=>true, 'accept'=>'image/*']) ?>
        <?php
        if ($model->image != '') {
            foreach($model->image as $img){ ?>
            <div style="float:left">  <img src="<?=Yii::getAlias('@web');?><?=$img['image'];?>"  class="gallery img-thumbnail" style="height:150px"><br>
             <?= Html::a('<span class="btn btn-danger">Delete</span>', Url::to(['/biodata-media/delete', 'id' => $img['id']]), ['data-method' => 'POST']) ?> </div>
                <?php 
                }
        }
        ?>    

actionUpdate控制器中的代码

public function actionUpdate($id)
    {
        $model = $this->findModel($id);     

      //  $media = BiodataMedia::find()->where(['biodata_id'=>$id])->all();
        $media = new BiodataMedia();        

        if ($model->load(Yii::$app->request->post()) ) {            

            $model->save();

             if($model->image !=''){
                $images = uploadedFile::getInstances($media,'image');               
                foreach($images as $image){
                $media = new BiodataMedia();
                $path = 'web/img/biodata/'.$image->basename.'.'.$image->extension;
                if($image->saveAs($path)){
                    $media = new BiodataMedia();
                    $media->image = $image->basename .'.'.$image->extension;
                    $media->image='/'.$path;
                    $media->biodata_id=$model->id;                 
                    $media->save();            
                }
               }

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


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

现在,我所面临的唯一问题是正确的重定向,即我只想刷新映像删除上的更新_form.php,我无法实现并寻找适当的解决方案。