gpt4 book ai didi

php - Yii2 数据库 session - 存储附加属性和用户信息

转载 作者:可可西里 更新时间:2023-11-01 00:30:01 24 4
gpt4 key购买 nike

我正在使用 Yii2 的 DBSession将 Web 应用程序 session 存储到名为 session 的数据库表中的类。

此表默认只有 3 列 - idexpiredata

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

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

最佳答案

创建迁移:

$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'.
],

关于php - Yii2 数据库 session - 存储附加属性和用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com