无法将模型 ID 传递给视图
Unable to pass model id to view
我正在使用 yii2
。我想将模型 ID 传递给视图,但这样做时出现错误。
控制器
public function actionProcess($id){
try {
$model = $this->findModel($id);
} catch (NotFoundHttpException $e) {
} // this will find my model/record based on the id
$file_name = $_GET['file_name'];
// $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional
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'=>$model->id]);
}
查看
在我看来,我正在尝试在提交按钮中传递模型 ID
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
</div>
</div>
现在,当我尝试访问我的 excel_options
视图时,出现以下错误。
Undefined variable: model
in E:\xampp\htdocs\inventory-web\backend\views\meteracceptanceheader\excel_options.php at line 103
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
更新 1
我用来渲染 excel_options
的控制器在
下面
public function actionProcess($id){
$model = $this->findModel($id);
// this will find my model/record based on the id
$file_name = $_GET['file_name'];
// $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional
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'=>$model->id]);
}
查看
<div class="box">
<!-- /.box-header -->
<div class="box-body">
<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'] ?>"/>
<h1>Maping</h1>
<div class="row">
<div class="col-md-2">
Ref #:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter MSN:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter Type:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2">
Sub-Div:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
</div>
</div>
</form>
</div>
</div>
如何将我的模型 ID 传递到视图中?
任何帮助将不胜感激
尝试传递自己的 $model 而不是 id。
return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'model'=>$model]);
并让您的代码可见。
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
我正在使用 yii2
。我想将模型 ID 传递给视图,但这样做时出现错误。
控制器
public function actionProcess($id){
try {
$model = $this->findModel($id);
} catch (NotFoundHttpException $e) {
} // this will find my model/record based on the id
$file_name = $_GET['file_name'];
// $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional
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'=>$model->id]);
}
查看
在我看来,我正在尝试在提交按钮中传递模型 ID
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
</div>
</div>
现在,当我尝试访问我的 excel_options
视图时,出现以下错误。
Undefined variable: model in E:\xampp\htdocs\inventory-web\backend\views\meteracceptanceheader\excel_options.php at line 103
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
更新 1
我用来渲染 excel_options
的控制器在
public function actionProcess($id){
$model = $this->findModel($id);
// this will find my model/record based on the id
$file_name = $_GET['file_name'];
// $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional
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'=>$model->id]);
}
查看
<div class="box">
<!-- /.box-header -->
<div class="box-body">
<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'] ?>"/>
<h1>Maping</h1>
<div class="row">
<div class="col-md-2">
Ref #:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter MSN:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter Type:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2">
Sub-Div:
</div>
<div class="col-md-4">
<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>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>
</div>
</div>
</form>
</div>
</div>
如何将我的模型 ID 传递到视图中?
任何帮助将不胜感激
尝试传递自己的 $model 而不是 id。
return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'model'=>$model]);
并让您的代码可见。
<a href="<?= URL::toRoute(['meteracceptanceheader/import', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Submit</a>