1

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

1
  • add code , please check Commented Jun 12, 2017 at 13:51

2 Answers 2

2

You need to use

Yii::$app->user->identity->id
Sign up to request clarification or add additional context in comments.

1 Comment

why? i use Yii::$app->user->id without any errors but now i have error? why change to array? i need fix this.
0

this problem for PHP version.

update to PHP 5.6 fix this problem.

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

1 Comment

Problem exists PHP 7.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.