/** * 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->goods_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * 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(); $sizes = ClothingSize::getAll(true, false); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'sizes' => $sizes]); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $this->validate($request, ['goods_name' => 'required|unique:goods|max:255', 'store_price' => 'required|numeric|min:0']); $goods = new Goods(); $goods->goods_name = $request->input('goods_name'); $goods->store_price = $request->input('store_price'); //$post->slug = Str::slug(Input::get('title')); $goods->cat_id = Input::get('cat_id'); $goods->content = Input::get('content'); $goods->user_id = Auth::user()->id; $goods->type_id = Input::get('type_id') ? Input::get('type_id') : null; //外键可以为空 $goods->brand_id = 1; //to-do if ($file = Input::file('thumb')) { $allowed_extensions = ["png", "jpg", "gif"]; if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) { return ['error' => '只能上传png, jpg, gif格式']; } $fileName = $file->getClientOriginalName(); $extension = $file->getClientOriginalExtension() ?: 'png'; $folderName = 'uploads/images/' . date("Ym", time()) . '/' . date("d", time()); $destinationPath = public_path() . '/' . $folderName; $safeName = str_random(10) . '.' . $extension; $file->move($destinationPath, $safeName); $goods->thumb = $folderName . '/' . $safeName; } //开始事务 DB::beginTransaction(); try { if ($goods->save()) { $thumbUrls = Input::get('thumbUrls'); $originalUrls = Input::get('originalUrls'); if (is_array($originalUrls)) { foreach ($originalUrls as $k => $originalUrl) { $photo = new Photo(); $photo->goods_id = $goods->id; $photo->original_url = $originalUrls[$k]; $photo->thumb_url = $thumbUrls[$k]; $photo->save(); } } //商品关联属性 $attrIdList = Input::get('attr_id_list'); $attrValueList = Input::get('attr_value_list'); $attrPriceList = Input::get('attr_price_list'); if (is_array($attrIdList)) { $attrs = array_map(null, $attrIdList, $attrValueList, $attrPriceList); foreach ($attrs as $_v) { $attr = new GoodsAttr(); $attr->goods_id = $goods->id; $attr->attr_id = $_v[0]; $attr->attr_value = $_v[1]; $attr->attr_price = $_v[2]; //如果为字符串attr_price存入数据库为0 if ($attr->goods_id && $attr->attr_id && $attr->attr_value) { $attr->save(); } } } DB::commit(); return Redirect::to('admin/goods'); } else { return Redirect::back()->withInput()->withErrors('保存失败!'); } } catch (\Exception $e) { DB::rollBack(); throw $e; } }