/** * 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]); }
public static function userGroups($user_id) { $dsn = Yii::$app->db->dsn; if (strtolower(substr($dsn, 0, 5)) == "mysql") { $members = Yii::$app->db->createCommand("SELECT GROUP_CONCAT(group_id) as id FROM " . GrpUsr::tableName() . "\n\t\t\t\tWHERE user_id = :id AND isdel = 0")->bindValues([":id" => $user_id])->queryScalar(); } else { $members = Yii::$app->db->createCommand("SELECT array_agg(group_id) as id FROM " . GrpUsr::tableName() . "\n\t\t\t\t\tWHERE user_id = :id AND isdel = 0")->bindValues([":id" => $user_id])->queryScalar(); } $groups = json_decode(str_replace(["{", "}"], ["[", "]"], $members)); return $groups == null ? [] : $groups; }
public function getMemberId() { $members = $this->db->createCommand("SELECT array_agg(user_id) as id FROM " . GrpUsr::tableName() . "\n\t\t\t\tWHERE group_id = :id AND isdel = 0")->bindValues([":id" => $this->id])->queryScalar(); return json_decode(str_replace(["{", "}"], ["[", "]"], $members)); }