dongyong1942 2018-04-24 15:04
浏览 39
已采纳

Yii2获取重定向的URL ID。 在创建行动

like I have a view index.php(Gridview)- linked to a Menu Manage Instructor.

from there I click on edit for a specific instructor.

The URL I get is like:

admin/user/update-instructor?id=11

In this page I have multiple tabs, one tab is instructor_schedule, which is again a grid view with add records button on top. I can add records clicking on add record without any issue.

My problem is now I want the form page redirect back to the page

admin/user/update-instructor?id=11

How I can achieve that?

I have tried like:

return $this->redirect(['user/update-instructor','id' => $model->id]);

and

return $this->redirect(['user/update-instructor','id' => $model->instructor_id]);

but I am getting the error missing information id.

Thanks.

action Create(ClassDurationController):

public function actionCreate() {
        $model = new ClassDuration();
    $count = count(Yii::$app->request->post('ClassDuration', []));

                $classdurations[] =new ClassDuration();
                for($i = 1; $i < $count; $i++) {
                $classdurations[] = new ClassDuration();
            }


    //if ($model->load(Yii::$app->request->post()) && $model->save()) {
             if (Model::loadMultiple($classdurations, Yii::$app->request->post()) && Model::validateMultiple($classdurations)) {

                  foreach ($classdurations as $classduration) {
                         // var_dump($classdurations);
                      $classduration->instructor_id=$_POST['ClassDuration'][0]['instructor_id'];
                      $classduration->save(false);
                      }                 

        Yii::$app->getSession()->setFlash('successClass');

        //return $this->redirect(['view', 'id' => $model->id]);
                    return $this->redirect(['user/update-instructor','id' => $model->id]);
    }

    return $this->render('create', [
        'model' => $model,
        'classdurations' => $classdurations,
    ]);
}

Action Update(ClassDurationController):

public function actionUpdate($id,$tab='information') {
        $model = $this->findModel($id);

                $wd_instructor = ClassDuration::find('instructor_id')->where(['id'=>$id])->One();
                $wd_instructor_id = $wd_instructor->instructor_id;
                $classdurations = ClassDuration::find()->where(['instructor_id'=>$wd_instructor_id])->all();

                if (Model::loadMultiple($classdurations, Yii::$app->request->post()) && Model::validateMultiple($classdurations)) {
                     foreach($classdurations as $classduration){
                        $classduration->location_id=$_POST['ClassDuration'][0]['location_id'];
                        $classduration->save(false);
                    }
            Yii::$app->getSession()->setFlash('successClass');

            // return $this->redirect(['view', 'id' => $model->id]);
                        return $this->redirect(['user/update-instructor', 'id' => $model->instructor_id, 'tab' => 'instructor_schedule']);
        }

        return $this->render('update', [
            'model' => $model,    
                        'workingdays' => $classdurations,
        ]);
    }

ActionUpdateInstructor:

public function actionUpdateInstructor($id,$tab='information') {
        $model = User::findOne($id);
        $uploadPath = 'web/instructor/' . $id;
        if (!file_exists($uploadPath)) {
            mkdir($uploadPath);
        }
        $profile = Instructor::find()->where(['user_id' => $id])->one();

        if ($profile) {
            $instructor_profile = $profile;
        } else {
            $instructor_profile = new Instructor;
            $instructor_profile->user_id = $id;
        }
        if ($id == 1) {
            $cls = 'hide';
        } else {
            $cls = '';
        }
        $title = "Update";

        $modelsRest = $model->rest;
        $modelsBreakTime = $model->breakTime;

        if (Yii::$app->request->isAjax && $model->load($_POST)) {
            Yii::$app->response->format = 'json';
            return \yii\bootstrap\ActiveForm::validate($model);
        }
        if (Yii::$app->request->isAjax && $instructor_profile->load($_POST)) {
            Yii::$app->response->format = 'json';
            return \yii\bootstrap\ActiveForm::validate($instructor_profile);
        }
        if ($model->load(Yii::$app->request->post()) && $instructor_profile->load(Yii::$app->request->post())) {

            $oldIDs = ArrayHelper::map($modelsRest, 'id', 'id');

            $modelsRest = Model::createMultiple(RestDays::classname(), $modelsRest);
            Model::loadMultiple($modelsRest, Yii::$app->request->post());

            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsRest, 'id', 'id')));

            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ArrayHelper::merge(
                    ActiveForm::validateMultiple($modelsRest),

                    ActiveForm::validate($model),
                    ActiveForm::validate($instructor_profile)
                );
            }

            if (trim($model->password) != '') {
                $model->setPassword($model->password);
            }

            $model->username = $model->email;

            $model->save();

            if ($model->user_role != '') {
                $assign = AuthAssignment::find()->where(['user_id' => $model->id])->One();
                $assign->item_name = $model->user_role;

                $assign->save();

            }
            $instructor_profile->file = UploadedFile::getInstance($instructor_profile, 'file');
            $instructor_profile->user_id = $model->id;

            if ($instructor_profile->file != '') {
                $instructor_profile->instructor_image = time() . '.' . $instructor_profile->file->extension;
            }

            $instructor_profile->save(false);

            if ($instructor_profile->file != '') {

                $uploadPath = 'web/instructor/' . $instructor_profile->user_id;
                if (!file_exists($uploadPath)) {
                    mkdir($uploadPath);
                }

                $instructor_profile->file->saveAs($uploadPath . '/' . $instructor_profile->instructor_image);

            }
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsRest) && $valid;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        if (!empty($deletedIDs)) {
                            RestDays::deleteAll(['id' => $deletedIDs]);
                        }

                        foreach ($modelsRest as $modelRests) {
                            $modelRests->instructor_id = $model->id;
                            if (!empty($modelRests->from_date) && !($flag = $modelRests->save(false))) {
                                $transaction->rollBack();
                                break;

                            }
                        }

                    }
                    if ($flag) {
                        $transaction->commit();
                        //return $this->redirect(['view-instructor', 'id' => $model->id]);
                                                return $this->redirect(['instructor']);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        }

        return $this->render('update_instructor', [
            'model' => $model,
            'modelsRest' => (empty($modelsRest)) ? [new RestDays] : $modelsRest,

            'instructor_profile' => $instructor_profile,
            'title' => $title,
            'cls' => $cls,

        ]);
    }
  • 写回答

1条回答 默认 最新

  • dpvomqeu396484 2018-04-25 05:19
    关注

    In your action Create(ClassDurationController) you assign to $model only

    $model = new ClassDuration();
    

    Maybe you should insert another variable to get id from parent or

    return $this->redirect(['user/update-instructor','id' => $_POST['ClassDuration'][0]['instructor_id']]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。