Update Complex Forms

Am creating complex forms. I have two Model Classes Courses and CourseStructure




    public function actionCreate()

    {

        $model = new Course();

        $request = Yii::$app->request;

        

    if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {

      //The course was created successfully, so we can use its data to make course structure

        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                return ActiveForm::validate($model);

        }

        if ($model->load(Yii::$app->request->post())) {  

      

        $model->attributes = $_POST['Course'];

        $model->course_start_date = date('Y-m-d', strtotime($_POST['Course']['course_start_date']));

        $model->created_at = new \yii\db\Expression('NOW()');


        $coursefile = UploadedFile::getInstance($model, 'coursefile');

        if(!is_null($coursefile))

        {

            foreach ($this->coursefile as $file) {


          $model->course_file_name = $coursefile->name;

          $ext = end((explode(".", $coursefile->name)));

          // generate a unique file name to prevent duplicate filenames

          $model->course_file_path = Yii::$app->security->generateRandomString().".{$ext}";

          Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/../web/data/course_admin_files/course_files/';

          $path = Yii::$app->params['uploadPath'] . $model->course_file_path;

          $file->saveAs($path);

        }//foreach

 

        }          

            $model->save(false);   

      

            if($model->save(false))

            //The course was created successfully, so we can use its data to make course structure

            {

              // check if topic format 

                if($model->course_format == Course::TYPE_TOPIC) 

                {      

                    for( $i = 1; $i <= $model->course_format_no; $i++ )

                    {

                      $structure = new CourseStructure();

                        $structure->course_id = $model->course_id;                  

                        $structure->structure_name = $model->course_format . $i;

                        $structure->structure_id = $i;


                        // fill in other course structure data here

                        $structure->save();

                    }

                }

            }                      

            else

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


                    

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



From CourseControllers, if course_format = TYPE_TOPIC, then based on the course_format_no selected,

some rows are added in course_structure table (CourseStructure) using




                if($model->course_format == Course::TYPE_TOPIC) 

                {      

                    for( $i = 1; $i <= $model->course_format_no; $i++ )

                    {

                      $structure = new CourseStructure();

                        $structure->course_id = $model->course_id;                  

                        $structure->structure_name = $model->course_format . $i;

                        $structure->structure_id = $i;


                        // fill in other course structure data here

                        $structure->save();

                    }

                }



7649

course_structure.PNG

It works fine in CourseCreate Action in CourseControllers

Now how do I do it for the CourseUpdate Action, so that if that either reduce the number of rows or increase it in course_structure table based on the course_format_no added in the update.

This what I have, but how do I improve it to update CourseStructure




    public function actionUpdate($id)

    {

        $model = $this->findModel($id);

        $old_model = $this->findModel($id);


    if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {

                \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                return ActiveForm::validate($model);

        }

        if ($model->load(Yii::$app->request->post())) {

        

        $model->attributes = $_POST['Course'];

        $model->course_start_date = Yii::$app->dateformatter->getDateFormat($_POST['Course']['course_start_date']);       


        $model->updated_by = Yii::$app->getid->getId();

        $model->updated_at = new \yii\db\Expression('NOW()');

        if($model->save(false))

            return $this->redirect(['view', 'id' => $model->course_id]);

            

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }

    }



I only write code for Courses, but dont know how to include course_structure into it.