【问题标题】:cakephp 3.x updating user datacakephp 3.x 更新用户数据
【发布时间】:2017-01-04 13:52:00
【问题描述】:

我已经编写了一个基本的登录脚本,现在需要更新存储在 auth 组件中的数据,然后将其保存到数据库中,这就是我目前所拥有的;

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.'));
    }
}

我尝试了几种不同的变体,但都无法奏效。有什么想法吗?

【问题讨论】:

  • 该代码肯定会触发错误,因为AuthComponent::user() 的返回值不是一个对象,而是一个数组。每当收到错误时,请将它们包含在您的问题中,包括完整的堆栈跟踪(最好从日志中以正确可读的方式复制),即使问题对于熟悉 CakePHP 的人来说可能很明显!

标签: php cakephp-3.0


【解决方案1】:

在 cakephp3 中更新数据与 cakephp2 略有不同,试试这样:

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 中更新记录的另一种方法是:

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

但我不推荐这个,因为不会触发回调。

【讨论】:

  • 这有点工作......它更新了数据库,但未更新身份验证组件。因此,完成此操作后,身份验证组件具有旧日期,而数据库具有新日期。此外,这种方式似乎是通过“get”通过另一个数据库查询使用更多内存。有没有办法更新 auth 组件中的数据,然后将其保存到数据库中?
  • 如果您需要同时更新 db 和 auth 组件,请尝试更新的答案。但这是更新 cakephp3 中记录的最佳方法,如文档中所述。
  • 谢谢,但更新后的答案不会更改存储在 auth 组件中的值 - 即,如果我执行 'echo $this->Auth->user('last_activity');'我得到的是旧值而不是新值。
  • 你在哪里做的 echo ?再看一次答案..应该给出更新的值。
  • 我已经很好地测试了这个..仍然不适合你?
【解决方案2】:

在 Cake3 中,您可以利用 afterIdentify 事件。

AppController::initialize中,为事件添加监听器:

\Cake\Event\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() 调用返回的数据应该始终是最新的,无需您付出任何额外的努力。

【讨论】:

  • 谢谢,我喜欢这个选项。但是,我仍然有Auth->user() 中的数据是旧数据而不是更新的last_activity 数据的问题
  • 您在代码中的确切位置访问此内容并获取旧信息?
  • 我在User::Account 后面的var_dump($this->Auth->user()); return $this->redirect($this->Auth->redirectUrl());
  • 好的,但这不是现实世界的用途,对吧?您即将重定向,当新 URL 被加载时,它将正确地在那里包含更新的信息。您是否有一个实际的用例,此时需要更新内存中的数据,还是只是一个测试?
  • 此时 url 重定向到具有var_dumpuser.account 方法。这是在 URL 重定向之后,但 last_activity 中的日期是旧日期,而不是保存在数据库中的新日期。
猜你喜欢
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 2017-11-20
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
相关资源
最近更新 更多