/** * Creates a new UserGroup model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new UserGroup(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function saveAll() { $__models = UserGroup::findAll(['userId' => $this->Id]); if (is_array($__models)) { $groups = $this->groupMap; foreach ($__models as $index => $model) { if (isset($groups[$index])) { $model->groupId = $groups[$index]; $flag = $model->save(); unset($groups[$index]); } else { $model->delete(); } if (!$flag) { break; } } foreach ($groups as $groupId) { //new insert $model = new UserGroup(); $model->groupId = $groupId; $model->userId = $this->Id; $flag = $model->save(); if (!$flag) { break; } } } else { //new insertion foreach ($this->groupMap as $groupId) { $model = new UserGroup(); $model->groupId = $groupId; $model->userId = $this->Id; $flag = $model->save(); if (!$flag) { break; } } } return $flag; }