/** * Updates an existing Gallery model. * If update is successful, the browser will be redirected to the 'view' page. * @param string $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { if (isset($_POST['deleteImg'])) { foreach ($_POST['deleteImg'] as $del_img) { $img = Image::findOne(['id' => $del_img]); $model->removeImage($img); } } $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); $model->upload(); return $this->redirect(['view', 'id' => $model->id]); } else { $images = $model->getImages(); return $this->render('update', ['model' => $model, 'imgs' => $images]); } }
/** * @param $id * @return array * @throws NotFoundHttpException * @throws InvalidParamException */ public function actionDelete($id) { $image = Image::findOne($id); $filePath = $image->getPathToOrigin(); if (!$image) { throw new NotFoundHttpException('Image not found'); } $product = Product::findOne($image->itemId); if (!$product) { throw new NotFoundHttpException('Product not found'); } $product->removeImage($image); if (file_exists($filePath)) { return ['error' => 'Ошибка удаления файла']; } else { Yii::$app->getResponse()->setStatusCode(204); } }
public function actionRemoveImage($id) { $model = Image::findOne($id); $itemId = $model->itemId; if ($model->delete()) { return $this->redirect(['update', 'id' => $itemId]); } else { return $model->errors; } }