ビヘイビアを使うことで実現できるようです。
ビヘイビアはイベントによって発火するようになり、DBのレコード新規作成時、更新時、ユーザーログイン時などのイベントを取れるようです。
namespace App\Model\Table;
use App\Model\Table\AppTable; // similar to AppController
class UsersTable extends AppTable
{
public function initialize(array $options)
{
parent::initialize($options);
// 例:親クラスが$this->addBehavior('Timestamp');を呼び出していて、さらにイベントを追加したい場合
if ($this->behaviors()->has('Timestamp') {
$this->behaviors()->get('Timestamp')->config([
'events' => [
'Users.login' => [
'last_login' => 'always'
],
],
]);
}
}
}
なお、FBでログインした際には上記は正常に動作しませんでした。
参考
https://book.cakephp.org/3.0/ja/orm/behaviors.html#id11
2017/07/01 追記
ビヘイビアはあくまでフィールとの値の自動更新のため、最終的にsave()で保存する必要があります。
https://book.cakephp.org/3.0/ja/controllers/components/authentication.html#id7
を元に拡張をしてみましょう。
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Username or password is incorrect'));
}
}
}
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$entity = $this->Users->get($user['id']); //Usersテーブルから該当するレコードを取得
$entity->dirty('modified', true); // modifiedのタイムスタンプは更新対象から除外する
$this->Users->touch($entity, 'Users.login'); //Users.loginのイベントを発火する
$this->Users->save($entity); //保存する
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('Username or password is incorrect'));
}
}
}
上記の Users.login は イベントを発火させてビヘイビアの方で設定したUsers.loginを呼んで last_loginのフィールドに now()をセットすると言う意味合いになります。
https://book.cakephp.org/3.0/ja/orm/behaviors/timestamp.html
https://stackoverflow.com/questions/32805900/cakephp-3-0-timestamp-behavior
https://www.grafikart.fr/forum/topics/23361
あたりが参考になります。


