/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Userdept();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Userdept'])) {
         $model->attributes = $_POST['Userdept'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['User'])) {
         $model->attributes = $_POST['User'];
         $model->pw_md5 = md5($_POST['User']['password']);
         if ($model->save()) {
             /* REMOVE PAST GRPS*/
             $pastGrps = Usergroup::model()->findAllByAttributes(array('uid' => $id));
             foreach ($pastGrps as $ug) {
                 $m = Usergroup::model()->findByPk($ug['id']);
                 $m->delete();
             }
             /* ADD NEW GRPS */
             if (isset($_POST['User']['g_id'])) {
                 $grps = $_POST['User']['g_id'];
                 foreach ($grps as $grp) {
                     $model1 = new Usergroup();
                     $model1->uid = $model->id;
                     $model1->gid = $grp;
                     $model1->save();
                 }
             }
             /* REMOVE PAST DEPTS*/
             $pastDepts = Userdept::model()->findAllByAttributes(array('uid' => $id));
             foreach ($pastDepts as $ud) {
                 $m = Userdept::model()->findByPk($ud['id']);
                 $m->delete();
             }
             /* ADD NEW DEPTS */
             if (isset($_POST['User']['dept_id'])) {
                 $depts = $_POST['User']['dept_id'];
                 foreach ($depts as $d) {
                     $model1 = new Userdept();
                     $model1->uid = $model->id;
                     $model1->dept_id = $d;
                     $model1->save();
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     } else {
         $grpsRow = Usergroup::model()->findAllByAttributes(array('uid' => $id));
         $grps = [];
         foreach ($grpsRow as $row) {
             $grps[] = $row['gid'];
         }
         $model->g_id = $grps;
         $deptsRow = Userdept::model()->findAllByAttributes(array('uid' => $id));
         $depts = [];
         foreach ($deptsRow as $row) {
             $depts[] = $row['dept_id'];
         }
         $model->dept_id = $depts;
     }
     $this->render('update', array('model' => $model));
 }