Ejemplo n.º 1
0
 /**
  * Creates a new Goods model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Goods();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates a new Shop model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     //vd($_POST);
     $model = new Goods();
     if ($model->load(Yii::$app->request->post())) {
         $model->image = Yii::$app->request->post('Goods')['new_image'];
         //vd(Yii::$app->request->post());
         //$model->validate();
         //vd($model->getErrors());
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 3
0
 /**
  * Creates a new Goods model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Goods();
     if ($model->load(Yii::$app->request->post())) {
         $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
         if ($model->imageFile) {
             $model->image = $model->imageFile->baseName . '.' . $model->imageFile->extension;
         }
         if ($model->save()) {
             $model->uploadImage();
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     $rubrics = ArrayHelper::map(Rubrics::find()->asArray()->indexBy('id')->all(), 'id', 'name');
     return $this->render('create', ['model' => $model, 'rubrics' => $rubrics]);
 }
Ejemplo n.º 4
0
 /**
  * @return mixed
  */
 public function actionCreate()
 {
     if (!$this->is_access('goods/create')) {
         Yii::$app->session->setFlash('error', $this->errorInfo);
         return $this->redirect($this->redirectUrl);
     }
     $model = new Goods();
     $GoodsGallery = new GoodsGallery();
     $post = Yii::$app->request->post();
     if ($model->load($post)) {
         $GoodsGallery->load($post);
         if ($model->validate() && $GoodsGallery->validate()) {
             // 商品封面图片
             if ($_FILES['Goods']['error']['goods_img'] === 0) {
                 $model->goods_img = File::uploadImage($model, 'goods_img', 'shop');
                 $model->goods_thumb = Images::thumb('./' . $model->goods_img, 250, 250);
             }
             if ($model->save(false)) {
                 // 更新商品货号
                 $model->editGoodsSn($model->goods_id);
                 // 商品相册
                 if ($_FILES['GoodsGallery']['error']['img_url'][0] === 0) {
                     $img_url = File::uploadImages($GoodsGallery, 'img_url', 'goods');
                     $GoodsGallery->addData($model->goods_id, $img_url);
                 }
                 // 商品属性
                 GoodsAttr::addData($model->goods_id);
             }
             Yii::$app->session->setFlash('success', '添加成功');
         }
         return $this->redirect(['index']);
     } else {
         // 商品分类
         $Category = new Category();
         $categoryList = $Category->parent(0, false);
         // 品牌
         $Brand = new Brand();
         $brandList = $Brand->getLists();
         // 商品类型
         $GoodsType = new GoodsType();
         $goodsTypeList = $GoodsType->dropList();
         Yii::$app->view->params['meta_title'] = '添加新商品';
         return $this->render('create', ['model' => $model, 'GoodsGallery' => $GoodsGallery, 'categoryList' => $categoryList, 'brandList' => $brandList, 'goodsTypeList' => $goodsTypeList]);
     }
 }
Ejemplo n.º 5
0
 public function actionAdd()
 {
     $model = new Goods();
     $model->setScenario('insert');
     if ($model->load(Yii::$app->request->post())) {
         $model->image = UploadedFile::getInstance($model, 'image');
         if ($model->validate()) {
             $model->cover = Yii::$app->security->generateRandomString(10) . '.' . $model->image->extension;
             if (floatval($model->price_original) < 0.01) {
                 $model->price_original = null;
             }
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 if (!$model->save(false)) {
                     throw new \Exception('商品添加失败!');
                 }
                 $filename = Yii::getAlias(Yii::$app->params['goods.coverPath']) . DIRECTORY_SEPARATOR . $model->cover;
                 if (!$model->image->saveAs($filename)) {
                     throw new \Exception('封面图片添加失败!');
                 }
                 // 记录商品库存
                 $goodsSurplus = new GoodsSurplus();
                 $goodsSurplus->goods_id = $model->id;
                 $goodsSurplus->surplus_before = 0;
                 $goodsSurplus->amount = $model->surplus;
                 $goodsSurplus->surplus_after = $model->surplus;
                 $goodsSurplus->remark = '初始化库存。';
                 if (!$goodsSurplus->save(false)) {
                     throw new \Exception('商品库存记录失败!');
                 }
                 $transaction->commit();
                 Yii::$app->session->setFlash('success', '成功添加商品“' . $model->name . '”。');
                 return $this->refresh();
             } catch (\Exception $e) {
                 $transaction->rollBack();
                 Yii::$app->session->setFlash('danger', $e->getMessage());
             }
         }
     }
     return $this->render('form', ['model' => $model]);
 }
Ejemplo n.º 6
0
 public function actionGoodsave()
 {
     //var_dump(Yii::$app->request->post("Goods"));
     //var_dump(Yii::$app->request->post());exit();
     /*$attr_ids = Yii::$app->request->post('attr_id_list');//arr
       $attr_vals = Yii::$app->request->post('attr_value_list');//arr
       var_dump($attr_ids);
       var_dump($attr_vals);exit();*/
     $model = new Goods();
     if ($model->load(Yii::$app->request->post())) {
         //上传商品图片并成生缩略图
         if ($_FILES['Goods']['name']['goods_img']) {
             $imgs = Yii::$app->imgload->UploadPhoto($model, 'uploads/goods/', 'goods_img', $isthumb = true);
             $img = explode('#', $imgs);
             $model->goods_img = $img[0];
             $model->goods_thumb = $img[1];
             if ($model->save()) {
                 //添加商品成功,获取属性并插入到商品属性关联表中
                 //获取新插入记录的id
                 $goods_id = $model->goods_id;
                 if ($goods_id) {
                     //实例化商品属性模型表
                     $attr = new GoodsAttr();
                     $attr_ids = Yii::$app->request->post('attr_id_list');
                     $attr_vals = Yii::$app->request->post('attr_value_list');
                     foreach ($attr_vals as $k => $v) {
                         if (!empty($v)) {
                             $attr->goods_id = $goods_id;
                             $attr->attr_id = $attr_ids[$k];
                             $attr->attr_value = $v;
                             $attr->save();
                         }
                     }
                     Yii::$app->getSession()->setFlash('info', '添加商品属性成功!');
                     return $this->redirect(['goodslist']);
                 } else {
                     Yii::$app->getSession()->setFlash("info", '添加商品属性失败');
                     return $this->redirect(['goodslist']);
                 }
                 Yii::$app->getSession()->setFlash('info', '添加商品成功!');
                 return $this->redirect(['goodslist']);
             } else {
                 Yii::$app->getSession()->setFlash('info', '添加失败!');
                 @unlink($model->goods_img);
                 @unlink($model->goods_thumb);
                 return $this->redirect(['goodslist']);
             }
             //不上传商品图片
         } else {
             if ($model->save()) {
                 //添加商品成功,获取属性并插入到商品属性关联表中
                 //获取新插入记录的id
                 $goods_id = $model->goods_id;
                 if ($goods_id) {
                     $attr_ids = Yii::$app->request->post('attr_id_list');
                     $attr_vals = Yii::$app->request->post('attr_value_list');
                     foreach ($attr_vals as $k => $v) {
                         if (!empty($v)) {
                             //实例化商品属性模型表
                             $attr = new GoodsAttr();
                             $attr->goods_id = $goods_id;
                             $attr->attr_id = $attr_ids[$k];
                             $attr->attr_value = $v;
                             $attr->save();
                         }
                     }
                     /*$model = new Model();
                           foreach ($post['models'] as $key => $item) {
                               $model->isNewRecord = true;
                               $model->setAttributes($item);
                               $model->save();
                               $model->id = 0;
                       }*/
                     Yii::$app->getSession()->setFlash('info', '添加商品成功!');
                     return $this->redirect(['goodslist']);
                 } else {
                     Yii::$app->getSession()->setFlash("info", '添加商品属性失败');
                     return $this->redirect(['goodslist']);
                 }
                 Yii::$app->getSession()->setFlash('info', '添加商品成功!');
                 return $this->redirect(['goodslist']);
             } else {
                 Yii::$app->getSession()->setFlash('info', '添加失败!');
                 return $this->redirect(['goodslist']);
             }
         }
     }
 }