Пример #1
0
 /**
  * Updates an existing Group model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $model->memberJson = json_encode($model->memberId);
     $post = Yii::$app->request->post();
     if (isset($post["Group"])) {
         $model->load($post);
         $transaction = Yii::$app->db->beginTransaction();
         try {
             if ($model->save()) {
                 $member = json_decode($post["Group"]["memberJson"]);
                 $gu = GrpUsr::deleteAll("group_id = :id", ["id" => $model->id]);
                 foreach ($member as $m) {
                     //$c = GrpUsr::find()->where(["group_id"=>$model->id,"user_id"=>intval($m)])->one();
                     //if (!$c)
                     //{
                     $c = new GrpUsr();
                     //}
                     $c->group_id = $model->id;
                     $c->user_id = intval($m);
                     $c->isdel = 0;
                     $c->save();
                 }
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $model->id]);
             } else {
                 $transaction->rollBack();
             }
         } catch (Exception $e) {
             $transaction->rollBack();
         }
     }
     return $this->render('update', ['model' => $model]);
 }