2017-01-04 1 views
0

기본 로그인 스크립트를 작성 했으므로 이제는 인증 구성 요소에 저장된 데이터를 업데이트 한 다음 데이터베이스에 저장해야합니다. 지금까지 내가 가지고있는 것입니다.cakephp 3.x 사용자 데이터 업데이트

public function login() 
{ 
    if ($this->request->is('post')) { 
     $user = $this->Auth->identify(); 
     if ($user) {     
      $this->Auth->setUser($user); 
      $this->Auth->user()->last_activity = date("Y-m-d"); 
      $this->Users->save($this->Auth->user()); 
      return $this->redirect($this->Auth->redirectUrl()); 
     } 
     $this->Flash->error(__('Email or password is incorrect, please try again.')); 
    } 
} 

몇 가지 변형을 시도했지만 효과가 없습니다. 어떤 아이디어?

public function login() 
{ 
if ($this->request->is('post')) { 
    $user = $this->Auth->identify(); 
    if ($user) {     
     $this->Auth->setUser($user); 
     $userData = $this->Users->get($user['id']); 
     $userData->last_activity = date("Y-m-d"); 
     if($this->Users->save($userData)){ 
      $user['last_activity'] = $userData->last_activity; // to update auth component 
     } 
     // echo $this->Auth->user('last_activity'); 
     return $this->redirect($this->Auth->redirectUrl()); 
    } 
    $this->Flash->error(__('Email or password is incorrect, please try again.')); 
} 
} 

cakephp3에서 기록을 갱신하는 또 다른 방법은 다음과 같습니다 : cakephp3에서

+1

'AuthComponent :: user()'의 반환 값이 객체가 아니라 배열이기 때문에이 코드는 오류를 유발합니다. 오류를 수신 할 때마다 CakePHP에 익숙한 사람들에게 문제가 명백 할지라도 전체 스택 추적 (적절하게 읽을 수있는 방식으로 사용할 수있는 로그에서 이상적으로 복사 됨)을 포함하여 질문에 포함하십시오. – ndm

답변

1

업데이트 데이터가 cakephp2보다 약간 다르다, 이런 식으로 뭔가를 시도

$query = $this->Users->query(); 
$query->update() 
->set(['last_activity ' => date('Y-m-d')]) 
->where(['id' => $user['id']]) 
->execute(); 

그러나 나는이 일을하지 않는 것이 좋습니다 콜백이 시작되지 않기 때문입니다.

+0

이것은 다소 효과가 있습니다 ... db를 업데이트하지만 인증 구성 요소는 업데이트되지 않습니다. 따라서이 작업이 완료되면 인증 구성 요소에는 이전 날짜가 있고 db에는 새 날짜가 있습니다. 또한이 방법은 'get'을 통해 다른 db 쿼리를 통해 더 많은 메모리를 사용하는 것으로 보입니다. auth 구성 요소의 데이터를 업데이트 한 다음 db에 저장하는 방법이 있습니까? – ChrisBull

+0

db 및 auth 구성 요소를 모두 업데이트해야하는 경우 업데이트 된 답변을 시도하십시오. 그러나 설명서에 언급 된대로 cakephp3의 레코드를 업데이트하는 가장 좋은 방법입니다. –

+0

감사하지만 업데이트 된 대답은 auth 구성 요소에 저장된 값을 변경하지 않습니다. 즉, 'echo $ this-> Auth-> user ('last_activity '); 나는 새로운 가치가 아니라 이전 가치를 얻는다. – ChrisBull

1

Cake3에서는 afterIdentify 이벤트를 이용할 수 있습니다. AppController::initialize에서

, 이벤트에 대한 리스너를 추가합니다

EventManager::instance()->on('Auth.afterIdentify', [$this, 'afterIdentify']); 

이벤트 처리 AppController::afterIdentify 기능을 추가 : 이제

public function afterIdentify(CakeEvent $cakeEvent, $data, $auth) { 
    $users_table = TableRegistry::get('Users'); 

    $user = $users_table->get($data['id']); 
    $user->last_activity = new Cake\I18n\FrozenTime(); 

    // If you ever need to do password rehashing, here's where it goes 
    if ($this->Auth->authenticationProvider()->needsPasswordRehash()) { 
     $user->password = $this->request->data('password'); 
    } 

    $users_table->save($user); 
} 

을의 Auth->user() 호출에 의해 반환 된 데이터는 항상 업해야 귀하의 추가 노력없이 최신 정보를 제공합니다.

+0

감사합니다.이 옵션이 마음에 듭니다. 그러나 여전히 Auth-> user()의 데이터가 업데이트 된 last_activity 데이터가 아닌 이전 데이터라는 문제가 있습니다. – ChrisBull

+0

코드에서 어디에서 이전 정보를 얻고 있습니까? –

+0

'return $ this-> redirect ($ this-> Auth-> redirectUrl());'User :: Account'에서'var_dump ($ this-> Auth-> user()); – ChrisBull

관련 문제