/** * Create init user */ public function actionAdmin() { echo "创建一个新用户 ...\n"; // 提示当前操作 $username = $this->prompt('User Name:'); // 接收用户名 $password = $this->prompt('Password:'); // 接收密码 $password_hash = Yii::$app->security->generatePasswordHash($password); $model = new Admin(); // 创建一个新用户 $model->admin_account = $username; // 完成赋值 $model->admin_pwd = $password_hash; if (!$model->save()) { foreach ($model->getErrors() as $error) { foreach ($error as $e) { echo "{$e}\n"; } } return 1; // 命令行返回1表示有异常 } return 0; // 返回0表示一切OK }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new Admin(); $user->fname = $this->fname; $user->lname = $this->lname; $user->contact_no = $this->contact_no; $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->company_name = $this->company_name; $user->company_description = $this->company_description; $user->shipping_address = $this->shipping_address; $user->generateAuthKey(); $user1 = new User(); $user1->fname = $this->fname; $user1->lname = $this->lname; $user1->contact_no = $this->contact_no; $user1->username = $this->username; $user1->email = $this->email; $user1->setPassword($this->password); $user1->company_name = $this->company_name; $user1->company_description = $this->company_description; $user1->shipping_address = $this->shipping_address; $user1->generateAuthKey(); if ($user->save() && $user1->save()) { return $user; return $user1; } } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new Admin(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { return $user; } } return null; }