/** * Creates a new Products model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Products(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function actionCreatenew() { $model = new Products(); $model->scenario = 'productsupload'; if ($model->load(Yii::$app->request->post())) { $model->FileUpload = UploadedFile::getInstance($model, 'FileUpload'); $k = 0; if ($model->FileUpload) { $time = time(); $model->FileUpload->saveAs(realpath(Yii::$app->basePath) . '/csv/' . $time . '.' . $model->FileUpload->extension); $model->FileUpload = realpath(Yii::$app->basePath) . '/csv/' . $time . '.' . $model->FileUpload->extension; $handle = fopen($model->FileUpload, "r"); $i = 0; $m = 0; while (($fileop = fgetcsv($handle, 1000, ",")) !== false) { if ($i >= 1) { /* products external images $model->externalFiles = file_get_contents($fileop[5]); $find = ".'\'."; $ent = str_replace ("/\\/","/",$fileop[5]); $a = "\\"; $a = preg_replace('/\\\\/','/',$fileop[5]); $getstrpos = strripos($a,"/"); $imageName = substr($fileop[5],$getstrpos+1); $time2 = time(); $img = realpath(Yii::$app->basePath).'/web/uploads/products/'.$time2.$imageName; $fileimageUrl = 'uploads/products/'.$time2.$imageName; file_put_contents($img, file_get_contents($fileop[5])); */ /* $model->externalFiles = UploadedFile::getInstance(file_get_contents($fileop[5])); $time2 = time(); $model->externalFiles->saveAs(realpath(Yii::$app->basePath).'/csv/' .$time2. '.' . $model->FileUpload->extension); $model->externalFiles = realpath(Yii::$app->basePath).'/csv/' .$time2. '.' . $model->FileUpload->extension; */ date_default_timezone_set('Asia/Calcutta'); $model->createdDate = date("Y-m-d H:i:s"); $model->updatedDate = date("Y-m-d H:i:s"); try { $userInfo = Yii::$app->db->createCommand()->insert('products', ['productCode' => $fileop[0], 'productName' => $fileop[1], 'categoryId' => Categories::getCategoryId($fileop[2]), 'description' => $fileop[3], 'status' => 'Active', 'createdDate' => $model->createdDate, 'updatedDate' => $model->updatedDate])->execute(); if ($userInfo) { $m++; } } catch (\Exception $e) { $duplicaterrors = $e->errorInfo[0]; if ($duplicaterrors == 23000) { $k++; } } } $i++; } if ($m > 0) { Yii::$app->getSession()->setFlash('success', 'Your are successfully Uploaded ' . $m . ' Products'); } if ($k > 0) { Yii::$app->getSession()->setFlash('error', 'Your Trying to add ' . $k . ' duplicate records'); } return $this->redirect(['createnew']); } } else { return $this->render('createfile', ['model' => $model]); } }