Невозможно передать идентификатор модели для просмотра - PullRequest
0 голосов
/ 20 октября 2018

Я использую yii2.Я хочу передать идентификатор модели в представление, но при этом я получаю сообщение об ошибке.

Контроллер

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]);

}

Просмотр

На мой взгляд, я пытаюсь передать идентификатор модели в кнопке отправки

 <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, я получаю сообщение об ошибке ниже.

Неопределенная переменная: модель в E: \ xampp \ htdocs \ inventory-web \ backend \ views \ meteracceptanceheader \ excel_options.php в строке 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>

Как я могу передать свой идентификатор модели в представление?

Любая помощь будет высоко ценится

1 Ответ

0 голосов
/ 20 октября 2018

Попробуй передать не id, а $ model.

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>
...