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

时间:2018-04-24 15:04:07

标签: php yii2

就像我有一个视图index.php(Gridview) - 链接到菜单管理教师。

从那里我点击特定教练的编辑。

我得到的网址如下:

admin/user/update-instructor?id=11

在这个页面中,我有多个标签,一个标签是instructor_schedule,这也是一个网格视图,顶部有添加记录按钮。 我可以添加记录,点击添加记录,没有任何问题。

现在我的问题是我希望表单页面重定向回页面

admin/user/update-instructor?id=11

我如何实现这一目标?

我试过了:

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

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

但我收到错误信息id

感谢。

动作创建(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,
    ]);
}

动作更新(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 个答案:

答案 0 :(得分:1)

在您的操作Create(ClassDurationController)中,您只分配给$ model

3 | FEMALE, MALE | YES, NULL
4 | FEMALE, MALE | NULL, NO

也许你应该插入另一个变量来从父或

获取id
$model = new ClassDuration();