/** * Creates a new Companies model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { if (Yii::$app->user->can('create-company')) { $model = new Companies(); $branch = new Branches(); if ($model->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post())) { // get the instance of the uploaded file $imageName = $model->company_name; if (!empty($model->file)) { $model->file = UploadedFile::getInstance($model, 'file'); $model->file->saveAs('uploads/' . $imageName . '.' . $model->file->extension); // save the path in the db column $model->logo = 'uploads/' . $imageName . '.' . $model->file->extension; } $model->company_created_date = date('Y-m-d h:m:s'); $model->save(); // save the branch $branch->companies_company_id = $model->company_id; $branch->branch_created_date = date('Y-m-d h:m:s'); $branch->save(); return $this->redirect(['view', 'id' => $model->company_id]); } else { return $this->render('create', ['model' => $model, 'branch' => $branch]); } } else { throw new ForbiddenHttpException(); } }
/** * Creates a new Companies model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { if (Yii::$app->user->can('create-company')) { $company = new Companies(); $branch = new Branches(); if ($company->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post())) { // print_r($company); // print_r($branch); // die; // get the instance of the uploaded file $imageName = $company->company_name; $company->file = UploadedFile::getInstance($company, 'file'); $company->file->saveAs('uploads/' . $imageName . '.' . $company->file->extension); // save the path in the db column $company->logo = 'uploads/' . $imageName . '.' . $company->file->extension; $company->company_created_date = date('Y-m-d h:i:s a'); $company->save(); // save branches $branch->branch_created_date = date('Y-m-d h:i:s a'); $branch->save(); return $this->redirect(['view', 'id' => $company->company_id]); } else { return $this->render('create', ['company' => $company, 'branch' => $branch]); } } else { throw new ForbiddenHttpException(); } }
/** * Creates a new Branches model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Branches(); if ($model->load(Yii::$app->request->post())) { $model->branch_created_date = date('Y-m-d h:m:s'); $model->save(); return $this->redirect(['view', 'id' => $model->branch_id]); } else { return $this->renderAjax('create', ['model' => $model]); } }
/** * Creates a new Branches model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { if (Yii::$app->user->can('create-branch')) { $model = new Branches(); if ($model->load(Yii::$app->request->post())) { $model->branch_created_date = date('Y-m-d h:i:s'); $model->save(); return $this->redirect(['view', 'id' => $model->branch_id]); } else { return $this->renderPartial('create', ['model' => $model]); } } else { throw new ForbiddenHttpException(); } }
/** * Creates a new Branches model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { if (Yii::$app->user->can('create-branch')) { $model = new Branches(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { echo json_encode(['status' => 'Success', 'message' => 'Cadastro realizado']); } else { echo json_encode(['status' => 'Error', 'message' => 'Falha no cadastro']); } } else { return $this->renderAjax('create', ['model' => $model]); } } else { throw new ForbiddenHttpException(); } }
/** * Creates a new Branches model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed * @throws ForbiddenHttpException */ public function actionCreate() { if (Yii::$app->user->can('create-branch')) { $model = new Branches(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { echo 1; } else { echo 0; } //return $this->redirect(['view', 'id' => $model->branch_id]); } else { return $this->renderAjax('create', ['model' => $model]); } } else { throw new ForbiddenHttpException(); } }
public function actionCreate() { if (Yii::$app->user->can('create-branch')) { $model = new Branches(); if ($model->load(Yii::$app->request->post())) { $model->branch_created_date = date('Y-m-d h:m:s'); if ($model->save()) { echo 1; } else { echo 0; } } else { return $this->renderAjax('create', ['model' => $model]); } } else { throw new ForbiddenHttpException('You do not have permission to create Branch', 403, null); } }
/** * Creates a new Companies model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { if (Yii::$app->user->can('create-company')) { $model = new Companies(); $branch = new Branches(); if ($model->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post())) { $model->company_created_date = date('Y-m-d H:i:s'); $model->save(); $branch->companies_company_id = $model->company_id; $branch->branch_created_date = date('Y-m-d H:i:s'); $branch->save(); return $this->redirect(['view', 'id' => $model->company_id]); } else { return $this->render('create', ['model' => $model, 'branch' => $branch]); } } else { throw new ForbiddenHttpException(); } }
public function actionReadfile() { $filename = 'in/employees.txt'; $readfile = fopen($filename, 'r'); while ($str = fgets($readfile, 1024)) { $items = explode(';', $str); //ФИО; Код; Регион; Должность; Примечание; Код Контрагента if ($items[0] == 'Наименование') { continue; } $branch = Branches::findOne(['branch_title' => $items[2]]); if (isset($branch)) { } else { $branch = new Branches(); $branch->branch_title = $items[2]; $branch->save(); } } fclose($readfile); return $this->redirect(['index']); }
/** * Creates a new Companies model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed * 1、上传文件 * 2、将company表单和branch表单放一起 */ public function actionCreate() { if (Yii::$app->user->can('create-company')) { $model = new Companies(); $branch = new Branches(); if ($model->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post())) { //文件上传 $imageName = $model->company_name; $model->file = UploadedFile::getInstance($model, 'file'); $model->file->saveAs('uploads/' . $imageName . '.' . $model->file->extension); //保存路径到数据库 $model->logo = 'uploads/' . $imageName . '.' . $model->file->extension; $model->save(); //保存branch $branch->companies_company_id = $model->company_id; $branch->save(); return $this->redirect(['view', 'id' => $model->company_id]); } else { return $this->render('create', ['model' => $model, 'branch' => $branch]); } } else { throw new ForbiddenHttpException(); } }
/** * Creates a new Companies model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Companies(); $branch = new Branches(); if ($model->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post())) { if (!empty($model->file)) { // get the instance of the uploaded file $imageName = date('Ymdhis'); $model->file = UploadedFile::getInstance($model, 'file'); $model->file->saveAs('uploads/' . $imageName . '.' . $model->file->extension); //save image name to db $model->logo = $imageName . '.' . $model->file->extension; } $model->company_created_date = date('Y-m-d h:i:s'); if ($model->save()) { $branch->companies_company_id = $model->company_id; $branch->branch_created_date = date('Y-m-d h:i:s'); $branch->save(); return $this->redirect(['view', 'id' => $model->company_id]); } } else { return $this->render('create', ['model' => $model, 'branch' => $branch]); } }
/** * Creates a new Companies model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Companies(); $branch = new Branches(); if (Yii::$app->user->can('create-company')) { if ($model->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post())) { $imageName = $model->company_name; $model->file = UploadedFile::getInstance($model, 'file'); if ($model->file) { $model->file->saveAs('uploads/' . $imageName . '.' . $model->file->extension); $model->logo = 'uploads/' . $imageName . '.' . $model->file->extension; } $model->save(); $branch->companies_company_id = $model->company_id; $branch->branch_created_date = date('Y-m-d'); $branch->save(); return $this->redirect(['view', 'id' => $model->company_id]); } else { return $this->render('create', ['model' => $model, 'branch' => $branch]); } } else { return $this->redirect(['index']); } }
public function actionImportExcel() { $inputFile = '/home/maan/Desktop/yii/advanced/uploads/branches_file.xlsx'; try { $inputFileType = \PHPExcel_IOFactory::identify($inputFile); $objReader = \PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFile); } catch (Exception $e) { die('Error '); } $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); for ($row = 1; $row <= $highestRow; $row++) { $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, false); // remove 1st column here // .. // .. // .. $branch = new Branches(); $branch->branch_name = $rowData[0][0]; $branch->branch_address = $rowData[0][1]; $branch->branch_created_date = $rowData[0][2]; $branch->branch_status = 'active'; // fixed. to be improved $branch->companies_company_id = 1; // fixed. to be improved $branch->save(); print_r($branch->getErrors()); } die; }
/** * 读取excel */ public function actionImportExcel() { $inputFile = 'uploads/branches_file.xlsx'; try { $inputFileType = \PHPExcel_IOFactory::identify($inputFile); $objReader = \PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFile); } catch (Exception $e) { die('error'); } $sheet = $objPHPExcel->getSheet(); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); for ($row = 1; $row <= $highestRow; $row++) { $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); if ($row == 1) { continue; } $branch = new Branches(); $branch_id = $rowData[0][0]; $branch->companies_company_id = $rowData[0][1]; $branch->branch_name = $rowData[0][2]; $branch->branch_address = $rowData[0][3]; $branch->branch_create_data = date('Y-m-d H:i:s'); $branch->branch_status = $rowData[0][4]; $branch->save(); print_r($branch->getErrors()); } die('success'); }