Ejemplo n.º 1
0
 public function actionDelete($id)
 {
     $product = Products::findOne($id);
     $characteristics = Characteristics::findOne($product->characteristics_id);
     $image = Images::findOne(['product_id' => $product->id]);
     $transaction = Products::getDb()->beginTransaction();
     try {
         if (!$product->delete()) {
             throw new \Exception('Product not deleted');
         }
         if (!$characteristics->delete()) {
             throw new \Exception('Characteristics not deleted');
         }
         if (!$image->delete()) {
             throw new \Exception('Image not deleted, from data base');
         }
         if (!unlink(Yii::getAlias('@webroot/' . AddProduct::THUMBS_IMAGES_PATH . $image->img_name))) {
             throw new \Exception('Thumb image not deleted');
         }
         if (!unlink(Yii::getAlias('@webroot/' . AddProduct::FULL_IMAGES_PATH . $image->img_name))) {
             throw new \Exception('Full image not deleted');
         }
         $transaction->commit();
         Yii::$app->session->addFlash('success', 'Product successfully deleted');
         $this->goBack();
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
 public function actionDelete($id)
 {
     $image = Images::findOne($id);
     if (!empty($image)) {
         $image->delete();
         unlink(Yii::getAlias('@webroot/' . AddImage::FULL_IMAGES_PATH . $image->name));
         unlink(Yii::getAlias('@webroot/' . AddImage::THUMBS_IMAGES_PATH . $image->name));
         Yii::$app->session->addFlash('success', 'Image successfully deleted');
         $this->goBack();
     } else {
         Yii::$app->session->addFlash('danger', 'Image with ' . $id . ' does not exist');
         $this->goBack();
     }
 }