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

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']]);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题