I am working on Yii2. I am importing an excel file. While importing it I am also sending the id of the previous model. The process is below
- The user opens the GUI and selects some values from the drop-down and then click on the create button.
Create Controller
public function actionCreate()
{
$model = new MeterAcceptanceHeader();
$model->prepared_by = Yii::$app->user->id;
$model->prepared_at = date('Y-m-d H:i:s');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['excel','id'=>$model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
-
After clicking on the next button user will be prompt to a new window in which he is asked to upload an excel file.
Excel Controller
public function actionExcel($id){ $file_name = "excel_" . Yii::$app->user->id . ".xlsx"; $error = ""; if(isset($_FILES['file'])) { $path_parts = pathinfo($_FILES["file"]["name"]); $extension = $path_parts['extension']; if(!in_array($extension,['xlsx','xls'])){ $error = "Invalid file"; }else { if (move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $file_name)) { $this->redirect([ 'process', 'file_name' => $file_name, 'header_no' => $_POST['header_no'], 'id'=>$id ]); } } } return $this->render("excel",['error'=>$error,'id'=>$id]); }Excel View
<div class="box-body"> <?php if($error != ""){?> <div class="alert alert-danger"><?=$error?></div> <?php } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>"/> <input id="btn" class="form-control" type="file" name="file" /> <div class="form-group"> <br /> Header Row <br /> <select name="header_no" class="form-control"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> </select> <br /> <input type="submit" value=" Next " class="btn btn-primary" /> </div> </form> </div> -
After uploading the excel file the user will click on next button
- Now at this window user will be asked to map the excel fields with the DB field
Process Controller
public function actionProcess(){
$file_name = $_GET['file_name'];
$id = $_GET['id'];
try {
$header_index = $_GET['header_no'];
$data = \moonland\phpexcel\Excel::widget([
'mode' => 'import',
'fileName' => 'uploads/' . $file_name,
'setFirstRecordAsKeys' => false, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
'setIndexSheetByName' => false, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
'getOnlySheet' => 0, // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
]);
if (isset($data[0])) {
$headers = $data[0][$header_index];
} else {
$headers = $data[$header_index];
}
}catch (Exception $x){
print_r($x->errorInfo);
}
return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'id'=>$id]);
}
Excel options view
<form action="import" method="post">
<input type="hidden" name="file_name" value="<?=$_GET['file_name']?>" />
<input type="hidden" name="header_index" value="<?= $_GET['header_no'] ?>"/>
<input type="hidden" name="model_id" value="<?= $_GET['id'] ?>"/>
<h1>Maping</h1>
<div class="row">
<div class="col-md-2">
Ref #:
</div>
<div class="col-md-4">
<label>
<select name="field[0][ref_no]" class="form-control">
<option value="">Select A field</option>
<?php foreach($headers as $k=>$v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?=$k?>"><?=$v?></option>
<?php } ?>
<?php } ?>
</select>
</label>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter MSN:
</div>
<div class="col-md-4">
<label>
<select name="field[0][meter_msn]" class="form-control">
<option value="">Select A field</option>
<?php foreach ($headers as $k => $v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</label>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter Type:
</div>
<div class="col-md-4">
<label>
<select name="field[0][meter_type]" class="form-control">
<option value="">Select A field</option>
<?php foreach ($headers as $k => $v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</label>
</div>
</div>
<div class="row">
<div class="col-md-2">
Sub-Div:
</div>
<div class="col-md-4">
<label>
<select name="field[0][sub_div]" class="form-control">
<option value="">Select A field</option>
<?php foreach ($headers as $k => $v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</label>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
<input type="submit" class="btn btn-primary pull-left" />
</div>
</div>
</form>
The above view is called in process action controller. And in this view, I am passing the model id as a hidden field.
-
After mapping the file and clicking on the submit button the data in the file will be saved into the database. Below is the import action which will save the data into the database
public function actionImport() { $file_name = $_POST['file_name']; $header_index = $_POST['header_index']; $fieldSet = $_POST['field']; $model_id = $_POST['model_id']; print_r($model_id); die(); . . . return $this->render('excel_finish', ['records_saved' => $ok_count,'status_arr'=>$status_arr]); }
What I have done so far
I am able to select data from the user (including the excel file). After mapping, it should go towards the import action but I am getting below error
Not Found (#404) Page not found.
url: http://localhost:225/inventory-web/backend/web/meteracceptanceheader/process/import
Update 1
While inspecting element in the browser I can see the model id.
I must be doing something wrong that I don't know.
Any help would be highly appreciated.
from Yii2- view gives me error after submitting an excel file


No comments:
Post a Comment