Yii2:无法更新多个图像

时间:2016-03-11 14:48:33

标签: php yii yii2

在控制器中

<div>
  <label for="yes">
    <input type="radio" name="radio1" value="yes" id="yes" class="option-yes">Yes
  </label>
  <label for="no">
    <input type="radio" name="radio1" value="no" id="no" class="option-no">No
  </label>
</div>
<br><br>
<div>
  <label for="refused">
    <input type="radio" name="radio2" id="refused" disabled> Refused
  </label>
  <label for="accomplished">
    <input type="radio" name="radio2" id="accomplished" disabled> Accomplished
  </label>
</div>

在规则部分

$('input[name="radio1"]').on('change', function() {

  if ($(this).hasClass('option-no')) {
    $('#refused').attr('checked', true);
    $('#accomplished').attr('checked', false);
  } else {
    $('#accomplished').attr('checked', true);
    $('#refused').attr('checked', false);
  }
});

尝试使用 public function actionUpdate($id) { $model = $this->findModel($id); 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) { $path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension; $file_instance->saveAs($path); $all_files_pathes []= $path; $model->images .= $path . ';'; } $model->save(false); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } } ,但它返回视图而图像没有变化。

上传多个图像文件并在数据库中存储路径,如

[['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4,'skipOnEmpty' => true, 'on' => 'update-photo-upload'],

我在scenario分隔的数据库中插入多个文件,无法更新图片的路径

actioncreate()

uploads/room_img/1059.jpg;uploads/room_img/1060.jpg;uploads/room_img/1064.jpg;

1 个答案:

答案 0 :(得分:0)

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $model->scenario = 'update-photo-upload';

    $oldFiles = explode( ";", $model->imageFiles);

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

       foreach ($oldFiles as $actFile)  {

               if (trim($actFile) != '') {
                 unlink( 'uploads/room_img/' . $actFile);
               }
        } 

        $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
        $all_files_paths = [];
             foreach ($model->imageFiles as $file_instance) 
             {
               $path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;
               $file_instance->saveAs($path);
                $all_files_pathes []= $path;
                $model->images .= $path . ';';
             }
    $model->save(false);
    return $this->redirect(['view', 'id' => $model->id]);
    } 

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