/** * 新增商品相册 * @param int $goods_id 商品id * @param array $imgs 图片路径数组 */ public function addData($goods_id, $imgs) { $sql = "INSERT INTO {{%goods_gallery}} (`goods_id`, `img_url`, `thumb_url`) VALUES "; foreach ($imgs as $val) { $thumb_url = Images::thumb('./' . $val, 180, 180); $sql .= "({$goods_id}, '{$val}', '{$thumb_url}'),"; } Yii::$app->db->createCommand(trim($sql, ','))->execute(); }
/** * @param integer $id * @return mixed */ public function actionUpdate($id) { if (!$this->is_access('goods/update')) { Yii::$app->session->setFlash('error', $this->errorInfo); return $this->redirect($this->redirectUrl); } $model = $this->findModel($id); $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)) { // 商品相册 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(); // 相册 $goodsGalleryList = $GoodsGallery->getGoodsGallery($id); Yii::$app->view->params['meta_title'] = '编辑商品'; return $this->render('update', ['model' => $model, 'GoodsGallery' => $GoodsGallery, 'categoryList' => $categoryList, 'brandList' => $brandList, 'goodsTypeList' => $goodsTypeList, 'goodsGalleryList' => $goodsGalleryList]); } }