Yii2 数据库会话 - 存储附加属性和用户信息 [英] Yii2 database session - store additional attributes and user information

查看:52
本文介绍了Yii2 数据库会话 - 存储附加属性和用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Yii2 的 DBSession 类来存储 web应用程序会话到一个数据库表中,称为session.

I'm using Yii2's DBSession class to store web application sessions into a database table, called session.

此表默认只有 3 列 - idexpiredata.

This table by default only has 3 columns - id, expire and data.

我想在此表中存储其他信息,例如登录用户的 user_id.

I'd like to store additional information into this table, like the user_id of a logged in user.

编辑:所以有一个名为 yii\web\MultiFieldSession 但没有关于如何使用它的例子.我会看看我能发现什么...

Edit: So there's a parent class called yii\web\MultiFieldSession but no examples about how it's used. I'll see what I can discover...

推荐答案

创建迁移:

$this->createTable('session', [
    'id' => $this->char(40)->notNull(),
    'expire' => $this->integer(),
    'data' => $this->binary(),
    'user_id' => $this->integer()
]);

$this->addPrimaryKey('session_pk', 'session', 'id');

将此添加到配置:

'components' => [
    'session' => [
        'class' => 'yii\web\DbSession',
        'writeCallback' => function($session){
            return [
                'user_id' => Yii::$app->user->id
            ];
        }
        // 'db' => 'mydb',  // the application component ID of the DB connection. Defaults to 'db'.
        // 'sessionTable' => 'my_session', // session table name. Defaults to 'session'.
    ],

这篇关于Yii2 数据库会话 - 存储附加属性和用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆