How to create user in database manually in Yii2?

前端 未结 2 1781
感动是毒
感动是毒 2021-01-25 11:35

I do import of users from a csv file to the database. In csv file I have some kinda username and password string. So how do I create a new user manually? As I understand I need

2条回答
  •  Happy的楠姐
    2021-01-25 12:00

    I think you forgot to set an active status of new User. You should check it in Login method of User model in your app, or in actionLogin of SiteController. For example, in my app the login method is:

    public function login()
    {
        if ($this->validate() and $this->getUser()->status !== User::INACTIVE_STATUS) {
           return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        }
        return false;
    }
    

    If you show your code we can help you faster)

    So if you are importing a list of users from a csv file, you will probably process this in a loop, try:

    foreach ($csv_data as $user_data) {
       $user = new User();
       $user->setPassword($password);
       $user->username = $user_data->username // or anything else, depending on your code
       $user->status = 1  // or = User::STATUS_ACTIVE, depending on your code
       $user->save();
    }
    

提交回复
热议问题