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;
     }
 }
Esempio n. 2
0
 public function confirmUpdate()
 {
     $transaction = Yii::$app->db->beginTransaction();
     $product = Products::findOne($this->id);
     $characteristic = Characteristics::findOne($product->characteristics_id);
     if (!($product && $characteristic)) {
         throw new NotFoundHttpException('Product Not Found');
     }
     try {
         $product->clk_name = $this->name;
         $product->clk_description = $this->description;
         $product->price = $this->price;
         if (!$product->save()) {
             throw new \Exception('Product not update, transaction rollback');
         }
         $characteristic->display_type = $this->displayType;
         $characteristic->mechanism_type = $this->mechanismType;
         $characteristic->starp_type = $this->starpType;
         $characteristic->sex = $this->sex;
         if (!$characteristic->save()) {
             throw new \Exception('Characteristic not update, transaction rollback');
         }
         $transaction->commit();
         Yii::$app->session->addFlash('success', 'Product successfully updated');
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
 protected function findModel($id)
 {
     if (($model = Products::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }