0

現在、認証コンポーネントを使用してログインしています。ユーザーがサイトにログインするたびに、users テーブルの last_login フィールドを更新したいと考えています。

私が持っているUsersコントローラーの私のログイン機能--

public function login() {
 $this->layout = 'main';
 if ($this->request->is('post')) {
 if($this->Auth->login()) {
   $this->redirect(array('controller'=>'pages','action'=>'dashboard'));  // after login , redirect on dahsboard
  }
  $this->Session->setFlash(__('Your username or password was incorrect.'));
  }
   $this->redirect(Router::url('/', true));  // there is no login.ctp file so it always redirect on home
}

アプリコントローラーで私は持っています

class AppController extends Controller {

public $components = array(
    'Auth',
    'Session',
);

function beforeFilter() {
    $this->Auth->loginAction = array(
        'controller' => 'users',
        'action' => 'login'
    );
    $this->Auth->logoutRedirect = array( 
       'controller' => 'pages',
      'action' => 'display','home'
     );
  }
4

1 に答える 1

0

これを追加して、ログインに成功した後、1 つの簡単な更新クエリを実行することをお勧めします。

$user = $this->Session->read("Auth.User");
$this->User->id = $user['id'];
$this->User->saveField('last_login', date('Y-m-d H:i:s'));

last_login フィールドを更新する方法は他にもいくつかあります。 1.

$data['User']['last_login']=date('Y-m-d H:i:s');
$this->User->save($data);

2.

$this->User->updateAll(array('User.last_login'=>date('Y-m-d H:i:s')),array('User.id'=>$user['id']));

この後、コードは次のようになります。

public function login() {
 $this->layout = 'main';
 if ($this->request->is('post')) {
 if($this->Auth->login()) {
       $user = $this->Session->read("Auth.User");
       $this->User->id = $user['id'];
       $this->User->saveField('last_login', date('Y-m-d H:i:s'));

   $this->redirect(array('controller'=>'pages','action'=>'dashboard'));  // after login , redirect on dahsboard
  }
  $this->Session->setFlash(__('Your username or password was incorrect.'));
  }
   $this->redirect(Router::url('/', true));  
// there is no login.ctp file so it always redirect on home
}
于 2015-10-09T05:43:31.013 に答える