/** * Update an existing User model. If update is successful, the browser * will be redirected to the 'view' page. * * @param string $id * @return mixed */ public function actionUpdate($id) { // set up user and profile $user = $this->findModel($id); $user->setScenario("admin"); $profile = $user->profile; // load post data and validate $post = Yii::$app->request->post(); if ($user->load($post) && $user->validate() && $profile->load($post) && $profile->validate()) { //Save user $user->save(false); // Save profile $profile->setUser($user->id)->save(false); // Remove old forms permissions FormUser::deleteAll(['user_id' => $user->id]); // Save new forms permissions $forms = Yii::$app->request->post('forms'); if (isset($forms) && is_array($forms)) { // Set news foreach ($forms as $form_id) { $formUser = new FormUser(); $formUser->form_id = $form_id; $formUser->user_id = $user->id; $formUser->save(); } } Yii::$app->getSession()->setFlash('success', Yii::t('app', 'The user has been successfully updated.')); return $this->redirect(['index']); } // Get all forms $forms = Form::find()->select(['id', 'name'])->orderBy('updated_at DESC')->all(); // Map id => name $forms = ArrayHelper::map($forms, 'id', 'name'); // Get forms of the selected user $userForms = FormUser::find()->select(['form_id'])->where(['user_id' => $user->id])->asArray()->all(); // Get only ids $userForms = ArrayHelper::getColumn($userForms, 'form_id'); // render return $this->render('update', ['user' => $user, 'profile' => $profile, 'forms' => $forms, 'userForms' => $userForms]); }