Add parameter to User on login with Events


I’d like to expand this post with Events, after login a user should have another parameter.

in my config file:

        'user' => [

            'identityClass' => 'common\models\Kundenstamm',

            'enableAutoLogin' => false,

			'on '.\yii\web\User::EVENT_AFTER_LOGIN => ['frontend\events\KundenstammEvents', 'handleAfterLogin'],


in my model

class Kundenstamm extends ActiveRecord implements IdentityInterface


    public $zeitraum;



in my KundenstammEvents

namespace frontend\events;

class KundenstammEvents {

    public static function handleAfterLogin($event)


		$event->identity->zeitraum = 2;



if I var_dump($event->identity) there directly, zeitraum is 2, but in my layout \Yii::$app->user->identity->zeitraum is empty :(

Does anyone see what is wrong there?


That because, I guess in $event, you don’t have the logged user… Just try to var_dump it to see what in it.

If you want to store the value in zeitraum which is a property of the User model/identity just do:

class KundenstammEvents {

    public static function handleAfterLogin($event)


		Yii::$app->user->zeitraum = 2;


It should work.

I tested this already:

object(yii\web\UserEvent)#162 (<img src='' class='bbc_emoticon' alt='8)' /> {


  object(common\models\Kundenstamm)#107 (10) {






    array(48) {


unfortunatly not.

Setting unknown property: yii\web\User::zeitraum

And if I change it to

Yii::$app->user->identity->zeitraum = 2; 

there is no error any more, but the value is empty in my template.

Did you implement the get / set method for zeitraum in the user model?

Just declaring the var should not work you need to setup 2 method in the model:

function setZeitraum($val) {



function getZeitraum($val) {

	return $this->zeitraum;


now you should be able to Yii::$app->user->identity->zeitraum = 2;

Done some investigation and I think is not possible to do what you want.

According to this issue is not possible to change the user identity data once initialized.

You should use session.

That is what I use now, and it works. And it is also better, because it doesn’t belong to the user.


To come back to this issue: Session is not really a solution for another case, because session expires after 1440 seconds (if session.gc_maxlifetime wasn’t changed), and if user logs in with marked to remember, session data is gone after some time… so I don’t know how tho handle this problem :(

Where do you store your users?

Use a table/model and add a zeitraum field there.

Looks like that this attribute (zeitraum) doesn’t have a corresponding column in “kundenstamm” table.

If it doesn’t, it’s volatile and will be reset to null every time you load the model from db.

The value set after login will soon be lost and you’ll get null in the next round of the requests.

Thanks for your replys. Actually, the zeitraum-example was another project which is ready ;D

Now I have a dropdown in my menu, where the user can select a company, which he is allowed to view. In yii1, I saved the allowed companys (id and name) after login in the user-cookie, so that I can easily create the dropdown in my layout with the cookie.

So of course there is no column in my user table like company_ids or something like that.