YII2 Session 在一次请求中保存多条记录

YII2 Session saving multiple record on one request

我正在尝试使用 Dbsession 来跟踪用户' activity 并且我根据 yii 文档设置了所有内容 运行 ,但是当用户加载页面时,多个会话记录被保存在一个请求中的数据库。下图显示了数据库中的数据这是什么原因以及解决此问题的任何解决方案? 在我的配置文件中我有这个

'session' => [
        // this is the name of the session cookie used for login on the frontend
        //'name' => 'advanced-frontend',
        'class' => 'yii\web\DbSession',
        'writeCallback' => function ($session) {
            return [
               'user_id' => \Yii::$app->user->id,
               'ip' => \Yii::$app->clientip->get_ip_address(),
           ];
        },

    ],

第一列(id)是主键并且应该是唯一的(它在migration). You have probably messed something with table schema - you should not be able to save 3 records with the same ID. DbSession is using upsert()中以这种方式声明并且依赖于id列的唯一性。

确保 id 列是主键,或者至少具有 UNIQUE 约束。