Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp 3.x更新用户数据_Php_Cakephp 3.0 - Fatal编程技术网

cakephp 3.x更新用户数据

cakephp 3.x更新用户数据,php,cakephp-3.0,Php,Cakephp 3.0,我已经编写了一个基本的登录脚本,现在需要更新auth组件中存储的数据,然后将其保存到数据库中,这就是我目前所拥有的 public function login() { if ($this->request->is('post')) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUse

我已经编写了一个基本的登录脚本,现在需要更新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.'));
    }
}

我试过几种不同的变体,但都没用。有什么想法吗?

在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();

但我不建议使用这种方法,因为回调不会被触发。

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

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

\Cake\Event\EventManager::instance()->on('Auth.afterIdentify', [$this, '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);
}
添加
AppController::afterIdentify
函数以处理事件:

\Cake\Event\EventManager::instance()->on('Auth.afterIdentify', [$this, '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()
返回的数据应该始终是最新的,而无需您付出任何额外的努力。

该代码肯定会触发错误,因为
AuthComponent::user()
的返回值不是对象,而是数组。无论何时收到错误,请将它们包括在您的问题中,包括完整的stacktrace(最好是从日志中以适当可读的方式复制出来的),即使对熟悉CakePHP的人来说问题可能很明显!这有点用。。。它会更新数据库,但不会更新auth组件。因此,完成此操作后,auth组件具有旧日期,db具有新日期。另外,这种方式似乎是通过另一个数据库查询通过“get”使用更多内存。没有办法更新auth组件中的数据,然后将其保存到db中吗?如果需要同时更新db和auth组件,请尝试更新的答案..但这是更新cakephp3中记录的最佳方法,如文档中所述。谢谢,但更新的答案不会更改auth组件中存储的值-如果我这样做的话'echo$this->Auth->user('last_activity');'我得到的是旧值,不是新值。你在哪里做的?再看一次答案。那应该会给出更新的值。我已经测试好了。仍然不适合你吗?谢谢,我喜欢这个选项。然而,我仍然有一个问题,那就是
Auth->user()
中的数据是旧数据,而不是更新的
last_活动
data您在代码中的什么地方访问这个并获取旧信息?我做
var_dump($this->Auth->user())返回$this->redirect($this->Auth->redirectUrl())之后的
用户::帐户中的
好的,但这不是真实世界的使用,对吗?您将要重定向,当加载新URL时,它将正确地在那里包含更新的信息。您是否有一个实际的用例需要在此时更新内存中的数据,或者它只是一个测试项目?此时url重定向到具有
var\u dump
user.account
方法。这是在url重定向之后,但
last_activity
中的日期是旧日期,而不是数据库中保存的新日期。