本文整理汇总了PHP中app\models\User::setAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP User::setAttributes方法的具体用法?PHP User::setAttributes怎么用?PHP User::setAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\User
的用法示例。
在下文中一共展示了User::setAttributes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
/**
* Registers a new user account.
* @return bool
*/
public function register()
{
if (!$this->validate()) {
return false;
}
$this->user->setAttributes(['email' => $this->email, 'username' => $this->username, 'password' => $this->password, 'firstName' => $this->firstName, 'lastName' => $this->lastName]);
return $this->user->register();
}
示例2: actionRegister
public function actionRegister()
{
$errors = null;
$model = new UserForm();
if (Yii::$app->request->isPost) {
$model->setAttributes(Yii::$app->request->post());
if ($model->validate()) {
$user = new User();
$user->setAttributes($model->getAttributes());
$user->setPassword($model->password);
$user->generateAuthKey();
$save = $user->save();
if ($save) {
$purse = new Purse();
$purse->user_id = $user->id;
$purse->active = 1;
$purse->balance = 0;
$purse->name = "Основной";
$purse->save();
$login = Yii::$app->user->login($user, 3600 * 24 * 14);
if ($login) {
return $this->goHome();
}
}
} else {
$errors = $model->getErrors();
}
}
return $this->renderPartial('register', ['errors' => $errors, 'model' => $model]);
}
示例3: create
/**
* @param $data array
* @param $password string
* @return User
*/
public function create($data, $password)
{
$user = new User();
$user->setAttributes($data);
$user->group = User::GROUP_USER;
$user->setPassword($password);
$user->authKey = Yii::$app->security->generateRandomString();
$this->saveOrFail($user);
return $user;
}
示例4: actionCreate
public function actionCreate()
{
$model = new User();
$model->setAction('admin-insert');
if (isset($_POST['save'])) {
$model->setAttributes($_POST['User']);
if ($model->validate() && $model->adminCreate()) {
Messages::get()->success("User created!");
$this->getRequest()->goToPage('users', 'index');
}
}
$this->assign('model', $model);
}
示例5: register
/**
* @return bool
*/
public function register()
{
if ($this->validate()) {
$user = new User();
if (User::SCENARIO_PERSON == $this->status) {
$user->setScenario($this->status);
} elseif (User::SCENARIO_COMPANY == $this->status) {
$user->setScenario($this->status);
} else {
return false;
}
$user->setAttributes($this->getAttributes());
$user->setPassword($this->password);
return $user->save();
}
return false;
}
示例6: actionCreateUser
public function actionCreateUser()
{
$this->stdout("Create a new user...\n", Console::BOLD);
$username = $this->prompt("Username: ", ['validator' => function ($input, &$error) {
if (preg_match('/^[\\w\\d_\\.]+$/', $input)) {
return true;
}
$error = "Username can only contain alphabet characters, numbers, dots and underscores.";
return false;
}]);
$name = trim($this->prompt("Display name: ", ['validator' => function ($input, &$error) {
$input = trim($input);
if (preg_match('/^[\\w\\d_\\. ]+$/', $input)) {
return true;
}
$error = "Display name must contain at least 1 visible character, and can only contain alphabet characters, numbers, dots, underscores and spaces. Leading and trailing spaces will be trimmed.";
return false;
}]));
$password = $this->prompt("Password: ", ['validator' => function ($input, &$error) {
if (preg_match('/^.{8,}$/', $input)) {
return true;
}
$error = "Password must have at least 8 characters";
return false;
}]);
$email = $this->prompt("Email: ", ['validator' => function ($input, &$error) {
if (filter_var($input, FILTER_VALIDATE_EMAIL)) {
return true;
}
$error = "Invalid email format";
return false;
}]);
$role = $this->prompt("Role (administrator/moderator): ", ['validator' => function ($input, &$error) {
if (preg_match('/^(administrator|moderator)$/', $input)) {
return true;
}
$error = "Role must be either 'administrator' or 'moderator'";
return false;
}]);
if ($this->confirm("Do you want to proceed with creating this user?")) {
$model = new User();
$model->scenario = User::SCENARIO_CONSOLE_CREATE;
$model->setAttributes(['username' => $username, 'name' => $name, 'email' => $email, 'status' => User::STATUS_ACTIVE]);
$model->setPassword($password);
$model->generateAuthKey();
$model->generateAccessToken();
if ($model->save()) {
$auth = Yii::$app->authManager;
$role = $auth->getRole($role);
$auth->assign($role, $model->id);
$this->stdout("User '{$username}' has been created\n", Console::BOLD);
} else {
foreach ($model->errors as $key => $errors) {
foreach ($errors as $error) {
$this->stdout("'{$key}': {$error}", Console::BOLD);
}
}
}
} else {
$this->stdout("Aborting...\n", Console::BOLD);
}
return 0;
}