/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new EntpProduct model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post())) { $model->loadDefaultValues(); $model->save(); if ($model->save()) { $produk = new EntpProduct(); $produk->product_id = $this->id; $produk->entrepreneur_user_id = Yii::$app->user->id; $produk->link('product', $model); } Yii::$app->session->setFlash('success', 'Create New Product Success'); return $this->redirect(['index']); //return $this->redirect(['view', 'entrepreneur_id' => $model->entrepreneur_id, 'product_id' => $model->product_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('success', 'Товар успешно создан!'); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $categories = Category::find()->all(); $model = new Product(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'categories' => $categories]); } }
public function actionCreate() { $model = new Product(); if ($model->load(\Yii::$app->request->post())) { if ($model->validate() && $model->save()) { return $this->redirect(['index']); } } return $this->render('form', ['model' => $model]); }
public function insert(ProductDto $productDto) { $product = new Product(); if ($product->load(['Product' => ArrayHelper::toArray($productDto)])) { $product->type = ArrayUtil::arrayToInt($productDto->type); $product->save(); return ArrayHelper::toArray($product); } else { return []; } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $categoryModel = new CategoryProduct(); $treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all()); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'treeParents' => $treeParents]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $currUser = Yii::$app->user->getIdentity(); $currMb = MerchantBrand::find()->where(['merchant_brand_id' => $currUser->username])->one(); $mbFk = $currMb->_id; $model = new Product(); $model->merchant_brand_fk = $mbFk; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => (string) $model->_id]); } else { return $this->render('create', ['model' => $model]); } }
public function simpan() { $model = new Product(); $model->product_name = $this->name; $model->product_price = $this->price; $model->product_category = $this->category; $model->product_created_date = date('Y-m-d'); $model->product_description = $this->description; $model->admin_id = Yii::$app->user->getId(); $model->product_slug = $this->slug; $model->save(); $this->uploadsAndSave(); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if (!is_numeric($model['agreement_id'])) { $agreement = Agreement::find()->where(["default_flag" => 1])->asArray()->one(); $model['agreement_id'] = $agreement['id']; } if ($model->load(Yii::$app->request->post()) && $model->save()) { $redirect = yii::$app->request->post('goto') == 'list' ? ['index'] : ['update', 'id' => $model->id]; return $this->redirect($redirect); } return $this->render('create', ['model' => $model]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $categoryModel = new ProductCategory(); $companyModel = new ProductCompany(); $treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all()); $treeCompany = ArrayHelper::map($companyModel->findAll(['status' => 10]), 'id', 'title'); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'treeParents' => $treeParents, 'treeCompany' => $treeCompany]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $model_category = new Category(); $model_product_category = new ProductCategory(); $model_manufacturer = new Manufacturer(); $model_product_manufacturer = new ProductManufacturer(); $upload_model = new UploadForm(); $resource = new Resources(); $resource_product = new ResourcesProduct(); if (isset($_POST['Product']) && !empty($_POST['Product'])) { $model->attributes = $_POST['Product']; $model->beforeSave(TRUE); if ($model->validate() && $model->save()) { if (empty($_POST['ProductCategory']['category_id'])) { $model_product_category->addError('category_id', 'No category selected.'); } else { foreach ($_POST['ProductCategory']['category_id'] as $category) { $model_product_category = new ProductCategory(); $model_product_category->product_id = $model->id; $model_product_category->category_id = $category; $model_product_category->save(); } } if (isset($_POST['ProductManufacturer']) && !empty($_POST['ProductManufacturer']['manufacturer_id'])) { $model_product_manufacturer->attributes = $_POST['ProductManufacturer']; $model_product_manufacturer->product_id = $model->id; $model_product_manufacturer->save(); } $upload_model->imageFiles = UploadedFile::getInstances($upload_model, 'imageFiles'); $uploaded_files = $upload_model->upload('product_image'); if (!empty($uploaded_files)) { foreach ($uploaded_files as $uploaded_file) { $resource = new Resources(); $resource->attributes = $uploaded_file; $resource->beforeSave(TRUE); if ($resource->validate() && $resource->save()) { $resource_product = new ResourcesProduct(); $resource_product->product_id = $model->id; $resource_product->resources_id = $resource->id; $resource_product->save(); } } } return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('create', ['model' => $model, 'model_manufacturer' => $model_manufacturer, 'model_product_manufacturer' => $model_product_manufacturer, 'model_category' => $model_category, 'model_product_category' => $model_product_category, 'upload_model' => $upload_model]); } }
/** * 保存商品 */ public function save() { #获得用户的地区信息用来提供浏览者的显示信息 $userinfo = \Yii::$app->user->getIdentity(); $newProduct = new Product(); $newProduct->product_country = 0; if (!empty($userinfo->province)) { $newProduct->product_province = $userinfo->province; } if (!empty($userinfo->city)) { $newProduct->product_city = $userinfo->city; } if (!empty($userinfo->area)) { $newProduct->product_area = $userinfo->area; } $newProduct->setAttributes($this->attributes); return $newProduct->save(); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { // echo Yii::getAlias('@common'); exit; $model = new Product(); $modelProdMeta = new ProductMeta(); $stores = []; if (\Yii::$app->user->identity->id == 1) { $stores = \common\models\UserStore::find()->select('store_id')->where(['user_id' => Yii::$app->user->identity->id])->all(); $stores = \yii\helpers\ArrayHelper::getColumn($stores, 'store_id'); $storeList = \common\models\Store::find()->where(['IN', 'store_id', $stores])->all(); } else { $storeList = \common\models\Store::find()->all(); } if ($postData = Yii::$app->request->post()) { if ($model->load($postData) && $model->save()) { $prod_id = $model->prod_id; foreach ($model->cat_id as $cat) { $catModel = new ProdCat(); $catModel->cat_id = $cat; $catModel->prod_id = $prod_id; $catModel->save(); } $file = UploadedFile::getInstances($model, 'images'); $model->file = $file[0]; $image_path = Yii::getAlias('@frontend') . '/web/uploads/product/' . $model->file->baseName . '_' . $prod_id . '.' . $model->file->extension; $model->file->saveAs($image_path); $this->updateProductMeta($id, 'prod_img', $model->file->baseName . '_' . $prod_id . '.' . $model->file->extension); foreach ($postData['PMeta'] as $key => $meta_value) { $this->updateProductMeta($id, $key, $meta_value); } return $this->redirect(['view', 'id' => $model->prod_id]); } } else { return $this->render('create', ['model' => $model, 'modelProdMeta' => $modelProdMeta, 'storeList' => $storeList]); } }
/** * batch import product * @param integer $id * @return mixed */ public function actionImport() { //if(!Yii::$app->user->can('viewYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); $format = Product::getImportExportFormat(); if (Yii::$app->request->post()) { $countCreate = $countUpdate = 0; $file = UploadedFile::getInstanceByName('importFile'); $handle = fopen($file->tempName, 'r'); $result = $this->inputCsv($handle); $arrData = []; if (count($result) <= 1) { Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'No Record, please check file.')); } else { // 将数据的key从数字变成固定的格式 for ($i = 1; $i < count($result); $i++) { $j = 0; foreach ($format as $item) { $data[$item] = $result[$i][$j]; $j++; } $data['thumbs'] = $result[$i][$j]; $data['images'] = $result[$i][$j + 1]; array_push($arrData, $data); } // 处理数据,如果ID大于0,则更新,否则新增 $line = 2; $errorLines = []; foreach ($arrData as $item) { if ($item['id'] > 0) { // 已存在的值,则更新数据,以及判断缩略图和图片 $model = Product::findOne($item['id']); if ($model === null) { array_push($errorLines, $line); $line++; continue; } foreach ($item as $k => $v) { if ($k == 'id' || $k == 'thumbs' || $k == 'images') { continue; } $model[$k] = iconv('gb2312', 'utf-8', trim($v)); } $result = $model->save(); if (!$result) { //如果保存失败 array_push($errorLines, $line); $line++; continue; } $countUpdate++; if ($item['thumbs'] && $item['images']) { $arrThumb = explode('|', $item['thumbs']); $arrImage = explode('|', $item['images']); $i = 0; $ids = []; foreach ($arrThumb as $thumb) { $thumb = trim($thumb); $image = trim($arrImage[$i]); $productImage = ProductImage::find()->where(['product_id' => $item['id'], 'thumb' => $thumb, 'image' => $image])->one(); if ($productImage) { //如果图片在数据库中已经存在,则假如到ids数组,防止后续被删除 array_push($ids, $productImage->id); } else { //不存在的话,新增记录并将id加入到ids $productImage = new ProductImage(['product_id' => $item['id'], 'thumb' => $thumb, 'image' => $image]); $productImage->save(); array_push($ids, $productImage->id); } $i++; } //删除在ids数组中记录 ProductImage::deleteAll(['and', 'product_id=' . $item['id'], ['not in', 'id', $ids]]); } } else { // 新的数据,插入,并将缩略图和图片插入 $model = new Product(); foreach ($item as $k => $v) { if ($k == 'id' || $k == 'thumbs' || $k == 'images') { continue; } $model[$k] = iconv('gb2312', 'utf-8', trim($v)); } // 将分类和品牌转换成对应的ID $category = Category::find()->where(['name' => trim($model->category_id)])->one(); $model->category_id = $category ? $category->id : 1; $brand = Brand::find()->where(['name' => trim($model->brand_id)])->one(); $model->brand_id = $brand ? $brand->id : 0; $result = $model->save(); if (!$result) { //如果保存失败 array_push($errorLines, $line); $line++; continue; } $countCreate++; if ($item['thumbs'] && $item['images']) { $arrThumb = explode('|', $item['thumbs']); $arrImage = explode('|', $item['images']); $i = 0; foreach ($arrThumb as $thumb) { $thumb = trim($thumb); $image = trim($arrImage[$i]); if ($thumb && $image) { // 缩略图和图片都有才加入 $productImage = new ProductImage(['product_id' => $model->id, 'thumb' => $thumb, 'image' => $image]); $productImage->save(); } $i++; } } } $line++; } if (count($errorLines)) { $strLine = implode(', ', $errorLines); Yii::$app->getSession()->setFlash('danger', Yii::t('app', "Line {strLine} error.", ['strLine' => $strLine])); } Yii::$app->getSession()->setFlash('success', Yii::t('app', "Import Data Success. Create: {countCreate} Update: {countUpdate}", ['countCreate' => $countCreate, 'countUpdate' => $countUpdate])); } } return $this->render('import', []); }
/** * @return \yii\db\ActiveQuery */ public function createObject() { $modelProduct = new Product(); $modelProduct->images_num = 3; $modelProduct->images_label = 'product'; $modelProduct->desc = ''; $modelProduct->price = 0; $modelProduct->user_id = Yii::$app->user->id; $modelProduct->temp = 1; $modelProduct->save(); Yii::$app->session->set('tempModel', 'Product'); Yii::$app->session->set('tempId', $modelProduct->id); return $modelProduct ? $modelProduct : null; }
private function _getProductData($url) { $url = trim($url); if (strlen($url) > 0 && !$this->is_404($url)) { $product_model = new Product(); $product_info = ['category_id' => '', 'name' => '', 'description' => '', 'size' => '', 'price' => 0.0, 'details' => '', 'original_url' => '', 'created_ts' => time()]; $category = Yii::$app->request->post('category'); if (!empty($category)) { $product_info['category_id'] = $category; } $imgdir = Yii::getAlias('@uploadPath') . '/products/'; $html = file_get_html($url); $product_info['original_url'] = $url; $product_name = $html->find('h1.top_title', 0); // Get Product name if (isset($product_name->innertext) && !empty($product_name->innertext)) { //Only save product if it has name $product_info['name'] = $product_name->innertext; $img = $html->find('img.lg_view', 0); //Get Product image $src = 'http:' . $img->src; $file_name = urldecode(end(explode('/', $img->src))); if (isset($img->src) && @copy($src, $imgdir . $file_name)) { $product_info['image'] = $file_name; } $price = $html->find('span.priceshow', 0); //Get Product size if (isset($price->innertext)) { $product_price = end(explode('$', $price->innertext)); $product_info['price'] = $product_price; } $description = $html->find('div.explain p', 1); //Get Product description if (isset($description->innertext)) { $product_info['description'] = trim($description->innertext); } $product_model->attributes = $product_info; if ($product_model->validate()) { if ($product_model->save()) { //return $this->redirect(['products/view/' . $product_model->id]); //return $this->refresh(); } else { Yii::$app->session->setFlash('saveErr', 'An error occurred. Please try again!'); } } else { Yii::$app->session->setFlash('saveErr', 'An error occurred. Please try again!'); } } } }
/** * 保存商品 */ public function save() { $newProduct = new Product(); $newProduct->setAttributes($this->attributes); return $newProduct->save(); }