错误数组到Yii :: $ app-> user-> id的字符串转换

时间:2017-06-12 12:51:02

标签: php yii yii2 yii2-advanced-app

我在项目中使用此 Yii :: $ app-&gt; user-&gt; id 来获取用户ID但在删除缓存后我遇到错误:数组到字符串转换< / p>

echo Yii :: $ app-&gt; user-&gt; id ['id']工作但是为什么要从Object更改为Array?

代码没有任何变化。

/ *删除工具栏小部件中的缓存* /

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

print_r(Yii :: $ app-&gt; user-&gt; id):

Array
(
    [id] = 152
    [email] = ad_ji_di@yahoo.com
)

错误:

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

2 个答案:

答案 0 :(得分:2)

您需要使用

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

答案 1 :(得分:0)

PHP版本的这个问题。

更新到PHP 5.6解决了这个问题。

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