/** * Создает новую модель товара. * Если создание прошло успешно - перенаправляет на просмотр. */ public function actionCreate() { $model = new Product(); if (Yii::app()->getRequest()->getIsPostRequest() && Yii::app()->getRequest()->getPost('Product')) { $attributes = Yii::app()->getRequest()->getPost('Product'); $typeAttributes = Yii::app()->getRequest()->getPost('Attribute', []); $variants = Yii::app()->getRequest()->getPost('ProductVariant', []); $categories = Yii::app()->getRequest()->getPost('categories', []); if ($model->saveData($attributes, $typeAttributes, $variants, $categories)) { $this->updateProductImages($model); Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('StoreModule.store', 'Record was added!')); $this->redirect((array) Yii::app()->getRequest()->getPost('submit-type', ['create'])); } else { Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('StoreModule.store', 'Failed to save product!')); } } $this->render('create', ['model' => $model]); }
/** * @return null|Product * @throws CDbException */ public function copy() { $transaction = Yii::app()->getDb()->beginTransaction(); $model = new Product(); try { $model->setAttributes($this->getAttributes()); $model->slug = null; $similarNamesCount = Yii::app()->getDb()->createCommand()->select('count(*)')->from($this->tableName())->where("name like :name", [':name' => $this->name . ' [%]'])->queryScalar(); $model->name = $this->name . ' [' . ($similarNamesCount + 1) . ']'; $attributes = $model->attributes; $typeAttributes = $this->getEavAttributes(); $variantAttributes = []; $categoriesIds = []; if ($variants = $this->variants) { foreach ($variants as $variant) { $variantAttributes[] = $variant->getAttributes(['attribute_id', 'attribute_value', 'amount', 'type', 'sku']); } } if ($categories = $this->categories) { foreach ($categories as $category) { $categoriesIds[] = $category->id; } } if (!$model->saveData($attributes, $typeAttributes, $variantAttributes, $categoriesIds)) { throw new CDbException('Error copy product!'); } $transaction->commit(); return $model; } catch (Exception $e) { $transaction->rollback(); } return null; }