Using MultiFieldSession class

With Yii 2.0.6 we have the new functionality to store data in an extra column in sessiontable when using database.

http://www.yiiframework.com/doc-2.0/yii-web-multifieldsession.html


    'session' => [

        'class' => 'yii\web\DbSession',

        'sessionTable' => '{{%session}}',

        'readCallback' => function ($fields) {

            return [

                'expireDate' => Yii::$app->formatter->asDate($fields['expire']),

            ];

        },

        'writeCallback' => function ($session) {

            return [

                'session_idp' => null,

                'user_id' => Yii::$app->user->id,

            ];

        }

    ],

The code above saves the id of the user in a new column in the sessiontable.

My question is: how can I set these attributes somewhere else. My goal is to set the session_idp in a controller as I will get this value from an external source. I also want to be able to clear the session data of all rows that has a given value in session_idp -column.

Does anyone know how to use this new functionality for what I want?

Or is it only possible to modify this value in the config-files?

My idea right now is to create a session-model that will handle this for me, but I am guessing there is a better way of doing this as of version 2.0.6?

I guess you can return empty array and do whatever you need in the callback.

Could you give me an example on how to use the callback when I am in a controller?

UPDATE:

After some extra research I have found out how to save the data to the extra column:




        $sessionIdp = $attributes['session_idp'][0];

        Yii::$app->session->writeCallback = function($session) use ($sessionIdp) {

            return ['session_idp' => $sessionIdp];

        };

I still struggle on how to get all sessions and clear all data in them from a given "session_idp".

Please let me know if I am doing something stupid or down right wrong here.

I’m greatful for any help.

For everyone else who may end up here looking for same thing. This is what I did.

I created a model for the session table and remove the data by calling the following:


Session::updateAll(['data' => ''], 'session_idp = :session_idp', [':session_idp' => $sessionIdp]);

That solved my problem and will automaticly log out all users that has the "session_idp" to "$sessionIdp".