Problem with User Identity

I’m trying to show the username when i’m logged but doesn’t work.

views/layouts/main.php


                

                '<li>'

                . Html::beginForm(['/site/logout'], 'post')

                . Html::submitButton(

                    'Logout (' . Yii::$app->user->identity->username . ')',

                    ['class' => 'btn btn-link']

                )

                . Html::endForm()

                . '</li>'



If i check the content of Yii::$app->user->identity i have:


object(app\models\User)#85 (13) { ["id"]=> NULL ["username"]=> NULL ["password"]=> NULL ["authKey"]=> NULL ["accessToken"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(9) { ["id"]=> int(1) ["username"]=> string(5) "admin" ["auth_key"]=> string(32) "1..." ["password_hash"]=> string(60) "$2y$13..." ["password_reset_token"]=> NULL ["email"]=> string(27) "mail@mail.com" ["status"]=> int(10) ["created_at"]=> int(1458584296) ["updated_at"]=> int(1458584296) } ["_oldAttributes":"...

The first "username" is NULL, if i check Yii::$app->user->identity->attributes:


 array(9) { ["id"]=> NULL ["username"]=> NULL ["auth_key"]=> string(32) "1..." ["password_hash"]=> string(60) "$2y$13..." ["password_reset_token"]=> NULL ["email"]=> string(27) "mail@mail.com" ["status"]=> int(10) ["created_at"]=> int(1458584296) ["updated_at"]=> int(1458584296) } 

Why doesn’t work like the yii2 app advanced? Thank you.

It should work like yii2 app advanced. Make sure that your user model class User extends ActiveRecord implements IdentityInterface just like the one in yii2 app advanced.

User model and controllers are the same because i made a copy :huh: .

I saw some differences between my app and advanced app of yii (var_dump(Yii::$app->user->identity)):

Yii app:


object(common\models\User)#85 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(9) { ["id"]=> int(1) ["username"]=> string(5) "admin" ["auth_key"]=> string(32) "1...

My app:


object(app\models\User)#85 (13) { ["id"]=> NULL ["username"]=> NULL ["password"]=> NULL ["authKey"]=> NULL ["accessToken"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(9) { ["id"]=> int(2) ["username"]=> string(4) "jiji" ["auth_key"]=> string(32) "1...

The main problem is the first variables that contains null values in my app, i don’t know why.