public function actionAddproduct() { $user = new AdminUser(); if (!$user->checkUserIsLogin()) { $this->redirect(Variable::$home_url); return; } $req = Yii::$app->request; //创建一个请求对象 $form = new ProductForm(); //产品表单 $sellerModel = new Seller(); //商家 $productCatModel = new ProductCategory(); //产品分类 $productMode = new Product(); //添加 $form->setScenario('create'); if ($form->load($req->post()) && $form->validate()) { $productLogoPath = 'uploads/'; if (!file_exists($productLogoPath)) { mkdir($productLogoPath, true); } $image = UploadedFile::getInstance($form, 'productLogo'); if (!($image instanceof UploadedFile && $image->error != UPLOAD_ERR_NO_FILE)) { $form->addError('sellerLogo', '请输入符合格式要求的图片'); return $this->render(Variable::$addProduct_view, ['model' => $form, 'sellerModel' => Seller::find()->all(), 'productCatModel' => ProductCategory::find()->all()]); } $ext = $image->getExtension(); $imageName = time() . rand(100, 999) . '.' . $ext; $isUpLoad = $image->saveAs($productLogoPath . $imageName); if (!$isUpLoad) { return $this->render(Variable::$addProduct_view, ['model' => $form, 'sellerModel' => Seller::find()->all(), 'productCatModel' => ProductCategory::find()->all(), 'error' => '图片上传失败']); } $form->productLogo = '/' . $productLogoPath . $imageName; if ($productMode->addProduct($form->productName, $form->productBrief, $form->productLogo, $form->sellerId, $form->categoryId, $form->productStock, $form->productPrice, $form->productStatus)) { $this->redirect(Variable::$productManger_url); return; } } return $this->render(Variable::$addProduct_view, ['model' => $form, 'sellerModel' => Seller::find()->all(), 'productCatModel' => ProductCategory::find()->all()]); }