简体   繁体   中英

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] = ad_ji_di@yahoo.com
)

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();
        }

在此处输入图片说明

您需要使用

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

this problem for PHP version.

update to PHP 5.6 fix this problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM