0

My user login with database is set up following this guide: http://www.bsourcecode.com/yiiframework2/yii-2-user-login-from-database/

Now my question is: how to implement last login feature so that every time user logs in, the appropriate database field is updated with current time?

I've set up rules in user model and also tried Timestamp Behavior, but no luck.

Thanks in advance!

jeesus
  • 459
  • 1
  • 7
  • 24

2 Answers2

1

Ended up using the login function where assigning date to user model was piece of cake.

$user->logintime = new Expression('NOW()');
$user->save();

The problem earlier was that I tried to implement it in the action of User model rather than in login form.

jeesus
  • 459
  • 1
  • 7
  • 24
0

Simple way to make it happen is in your login method just add touch

function login(){

    // check login
    if(//login true)
    {
        $user = User::findIdentity(Yii::$app->user->id);
        $user->touch('lastlogin_at'); // Datatable column name that you want to update the time.
    }

}

See more about touch() over here

Sahil Patel
  • 1,425
  • 13
  • 29