user3770797
user3770797

Reputation: 374

Error Array to string conversion for Yii::$app->user->id

I use this Yii::$app->user->id in my project for getting the user id but after I delete the cache I have an error: Array to string conversion

echo Yii::$app->user->id['id'] is work but why change from Object to Array?

Without any change in the code.

/* Remove Cache In Toolbar Widget */

Yii::$app->cache->delete('toolbar_model_user-'.Yii::$app->user->id);
Yii::$app->cache->delete('toolbar_blogModel_blog-'.$this->blogId);

print_r (Yii::$app->user->id):

Array
(
    [id] = 152
    [email] = [email protected]
)

Error:

Array to string conversion

    1. in /home/public_html/app/vendor/yiisoft/yii2/web/User.php at line 245
    236237238239240241242243244245246247248249250251252253254

         * @return bool whether the user is logged in
         */
        public function login(IdentityInterface $identity, $duration = 0)
        {
            if ($this->beforeLogin($identity, false, $duration)) {
                $this->switchIdentity($identity, $duration);
                $id = $identity->getId();
                $ip = Yii::$app->getRequest()->getUserIP();
                if ($this->enableSession) {
                    $log = "User '$id' logged in from $ip with duration $duration.";
                } else {
                    $log = "User '$id' logged in from $ip. Session not enabled.";
                }
                Yii::info($log, __METHOD__);
                $this->afterLogin($identity, false, $duration);
            }

            return !$this->getIsGuest();
        }

enter image description here

Upvotes: 1

Views: 4308

Answers (2)

user3770797
user3770797

Reputation: 374

this problem for PHP version.

update to PHP 5.6 fix this problem.

https://github.com/yiisoft/yii2/issues/1029

Upvotes: 0

Sersh
Sersh

Reputation: 308

You need to use

Yii::$app->user->identity->id

Upvotes: 2

Related Questions