public function save() { $transaction = Yii::$app->db->beginTransaction(); try { $characteristics = new Characteristics(); $characteristics->display_type = $this->displayType; $characteristics->mechanism_type = $this->mechanismType; $characteristics->starp_type = $this->starpType; $characteristics->sex = $this->sex; if (!$characteristics->save(false)) { throw new \Exception('Charasteristic not save, transaction rollback'); } $products = new Products(); $products->clk_name = $this->name; $products->clk_description = $this->description; $products->characteristics_id = $characteristics->id; $products->price = $this->price; if (!$products->save(false)) { throw new \Exception('Product not save, transaction rollback'); } $hashName = Yii::$app->security->generateRandomString(); $fullImagePath = self::FULL_IMAGES_PATH . $hashName . '.' . $this->images->extension; if (!$this->images->saveAs($fullImagePath)) { throw new \Exception('Image not save in full image path'); } $imgSizeReduct = function ($side = 'width') use($fullImagePath) { $size = getimagesize($fullImagePath); if ($side === 'width') { return $size[0] / self::THUMB_REDUCTION; } if ($side === 'height') { return $size[1] / self::THUMB_REDUCTION; } }; $images = new Images(); $transformation = new Transformation(); $imagine = new Imagine(); $transformation->thumbnail(new Box($imgSizeReduct('width'), $imgSizeReduct('height')))->save(Yii::getAlias('@webroot/' . self::THUMBS_IMAGES_PATH . $hashName . '.' . $this->images->extension)); $transformation->apply($imagine->open(Yii::getAlias('@webroot/' . self::FULL_IMAGES_PATH . $hashName . '.' . $this->images->extension))); $images->product_id = $products->id; $images->img_name = $hashName . '.' . $this->images->extension; if (!$images->save(false)) { throw new \Exception('Images not save, transaction rollback'); } $transaction->commit(); Yii::$app->session->addFlash('success', 'Product successfully added'); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } }
public function actionIndex($id) { $model = Characteristics::model()->findAll('category_id = :num', array(':num' => $id)); $models = new Characteristics(); $list = $this->listDown($id); if (isset($_POST['Characteristics'])) { //echo '<pre>'; //print_r($_POST); //echo '</pre>'; $models->attributes = $_POST['Characteristics']; $models->category_id = $id; if ($models->save()) { $this->refresh(); } } $this->render('index', array('model' => $model, 'models' => $models, 'list' => $list, 'idk' => $id)); }