Example #1
0
 /**
  * Creates a new Group model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Group();
     $model->time = date('Y-m-d H:i:s');
     $model->isdel = 0;
     $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 = 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('create', ['model' => $model]);
 }