private function getGroups() { if ($this->_groups === null) { $this->_groups = Group::find()->all(); } return $this->_groups; }
public function getGroups() { return $this->hasMany(Group::className(), ['id' => 'group_id'])->viaTable('admin_user_group', ['user_id' => 'id']); }
/** * Create a new user and append them to an existing group. * * @return boolean */ public function actionUser() { while (true) { $email = $this->prompt('User E-Mail:'); if (!empty(User::findByEmail($email))) { $this->outputError('The provided E-Mail already exsists in the System.'); } else { break; } } $titleArray = ['Mr' => 1, 'Mrs' => 2]; $title = $this->select('Title:', $titleArray); $firstname = $this->prompt('Firstname:'); $lastname = $this->prompt('Lastname:'); $password = $this->prompt('User Password:'******'{$email}'?") !== true) { return $this->outputError('Abort user creation process.'); } $user = new User(); $user->email = $email; $user->password_salt = Yii::$app->getSecurity()->generateRandomString(); $user->password = Yii::$app->getSecurity()->generatePasswordHash($password . $user->password_salt); $user->firstname = $firstname; $user->lastname = $lastname; $user->title = $titleArray[$title]; if (!$user->save()) { throw new Exception("Unable to create new user."); } $groupSelect = []; foreach (Group::find()->all() as $entry) { $groupSelect[$entry->id] = $entry->name . ' (' . $entry->text . ')'; $this->output($entry->id . ' - ' . $groupSelect[$entry->id]); } $groupId = $this->select('Select Group the user should belong to:', $groupSelect); $this->insert('admin_user_group', ['user_id' => $user->id, 'group_id' => $groupId]); return $this->outputSuccess("The user ({$email}) has been created."); }