CakePHP 3.0时间戳行为

bol*_*has 2 timestamp cakephp behavior cakephp-3.0

我想在用户登录时更新users表中的时间戳.我创建了一个名为'lastLogin'的日期时间字段.

从我的用户控制器的登录操作我打电话:

$user = $this->Auth->identify();
if ($user) {
     $this->Auth->setUser($user);
     $this->Users->touch($this->Users->get($user['id']), 'Users.afterLogin');
}
Run Code Online (Sandbox Code Playgroud)

在我的用户表中,我有:

$this->addBehavior('Timestamp', [
    'events' => [
        'Model.beforeSave' => [
            'created' => 'new',
            'modified' => 'always',
        ],
        'Users.afterLogin' => [
            'lastLogin' => 'always'
        ]
    ]
]);
Run Code Online (Sandbox Code Playgroud)

我已经测试过触发了事件并且正在更新实体属性.但是它不会保存到数据库中.

是这个意图,即我必须明确保存实体,还是我错过了什么?

谢谢!

安德

AD7*_*six 5

该行为仅更新该字段

文档中可以清楚地看出,但代码只更新了字段值.该行为实际上不会更新数据库,有效地假设将在稍后的同一请求中存储调用.