0

Hello World!
i created in the user table a last_activity to check when the user logged in, how to change the value to 0000-00-00 00:00:00 after a user logout.

 public function actionLogout()
    {

        $event = $this->getUserEvent(\Yii::$app->user->identity);
      /*  $account = $this->finder->findAccount()->byClient($client)->one();
        $account->user->updateAttributes(['last_activity' => '0000-00-00 00:00:00']);*/
        $this->trigger(self::EVENT_BEFORE_LOGOUT, $event);

       $logout = \Yii::$app->getUser()->logout();
       /* if ($logout){
            $module = \Yii::$app->getModule('user');
            $module->updateAttributes(['last_activity'=>'0000-00-00 00:00:00']);
        }*/
        $this->trigger(self::EVENT_AFTER_LOGOUT, $event);
      //  \Yii::$app->user->updateAttributes(['last_activity' => 0]);
      //  User::deleteLast();
    //    \Yii::$app->user->updateAttributes(['last_activity' => '0000-00-00 00:00:00']);
       //  $this->updateAttributes(['last_activity' => '0000-00-00 00:00:00']);
       // $this->user->updateAttributes(['last_activity' => null]);
       // return Yii::$app->user->updateAttributes(['last_ip'=> 1]);
          return $this->redirect(site/index);
    }

View cabinet-panel.php

  <?php
        echo Menu::widget([
            'items' => [
               ////
                [
                    'label' => Yii::t('main', "Logout"),
                    'url' => null,
                    'options' => ['class' => 'li-logout'],
                    'template' => '<a id="link-logout">{label}</a>'
                ],
            ],
            'options' => [
                'class' => 'nav nav-pills nav-stacked'
            ],
            'activeCssClass' => 'active',
            'route' => Yii::$app->request->url,
            'activateItems' => true,
        ]);
        ?>


common.js here is the function, that works when a user clicks on the anchor tag

function logout() {

    $(document).on('click', '#link-logout', function () {
        $.post(getLang() + '/user/security/logout/');
    });

}

logout();

WardNsour
  • 313
  • 1
  • 2
  • 16
  • 0000-00-00 00:00:00 :) return a query in log out and save logout date in db. –  Mar 04 '20 at 12:09
  • You can update with `Yii::$app->user->updateAttributes(['last_ip'=> 1, 'last_activity'=>'0000-00-00 00:00:00']);` if both are in same table? – Insane Skull Mar 04 '20 at 12:21
  • the last line is not working , i am sorry i didn't explain it's not working for the last_ip – WardNsour Mar 04 '20 at 12:28
  • @Dlk would you sir please explain how to do this ? – WardNsour Mar 04 '20 at 12:29
  • I dont understand Yii much, so, I cant give you as a complete answer sorry! do an update query in your `public function actionLogout()` with userid and update value you want to change in database. something like this : `$user->status = 2; $user->save(true, ['status']);` status is the value you want to change in my example. this might help https://stackoverflow.com/questions/34753487/how-do-i-set-the-user-status-online-offline-in-a-yii-2-application –  Mar 04 '20 at 12:42
  • i tried to do something like this yesterday but it didn't work ` function logout() { $(document).on('click', '#link-logout', function () { $.post(getLang() + '/user/security/logout/'); }); $.ajax({ url: getLang() + 'user/security/logout', type: 'POST', data : { 'last_activity' : '000000000000000' }, dataType: 'json', success: function (data) { data['user']['last_activity'] = 'last_activity' } }) } logout();` – WardNsour Mar 04 '20 at 12:44
  • That wont work! because you are mixing ajax in php, both are different programing language, add `var userid = "";` to your ajax code and post it to logout.php then do query like I said in my previous comment. in this function `public function actionLogout()` not in this one `function logout() {` –  Mar 04 '20 at 12:53
  • what is the type for the column `last_activity` datetime? – Muhammad Omer Aslam Mar 04 '20 at 20:13
  • @MuhammadOmerAslam yeah it's bro :D – WardNsour Mar 04 '20 at 20:25
  • what is the default value set for the column – Muhammad Omer Aslam Mar 04 '20 at 21:15
  • ` Yii::$app->User->updateAttributes(['last_activity' => date('Y/m/d h:i:s a', time())]) ` – WardNsour Mar 05 '20 at 08:44
  • @MuhammadOmerAslam i want to use this value later to show if the user is online or offline.. if that makes sense sir :) – WardNsour Mar 05 '20 at 08:45
  • @MuhammadOmerAslam the type is datetime(6) and the default is None – WardNsour Mar 05 '20 at 08:47
  • @MuhammadOmerAslam Please sir i can't wait for your answer i have seen your profile and i really respect your knowledge – WardNsour Mar 06 '20 at 19:53

0 Answers0