D
D
Denis2018-02-20 01:18:45
Yii
Denis, 2018-02-20 01:18:45

Pass user id on registration to token table?

There are two tables (models) user and token. User data is stored in user, id, email, username, etc., and in token, email confirmation tokens, password changes.
There is a RegistrationForm layer model, with a function for registration:

public function registration()
    {
        if (!$this->validate()) {
            return null;
        }
        $user = new User();
        $user->nickname = $this->nickname;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
         /.....
        return $user->save() ? $user : null;
    }

How to write in the table token, token code and id of the current registered user during registration?
Write a function for a token?
public function createEmailConfirmToken()
    {
        $token = new Token();
        $token->code = Yii::$app->security->generateRandomString();
        $token->created_at = time();
        $token->type = Token::CONFIRMATION_EMAIL;

        return $token->save() ? $token : null;
    }

How then to transfer the user ID?
Please describe how to implement it correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2018-02-20
@valetu

You can return user_id from the registration method. The ID of an AR object is called after saving. Those:

if($user->save()){
    return $user->id;
}
return null;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question