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']]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)