【问题标题】:Error Array to string conversion for Yii::$app->user->idYii::$app->user->id 的错误数组到字符串转换
【发布时间】:2018-08-02 20:34:43
【问题描述】:

我在我的项目中使用这个 Yii::$app->user->id 来获取用户 ID,但是在我删除缓存后出现错误:数组到字符串的转换

echo Yii::$app->user->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->user->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();
        }

【问题讨论】:

  • 添加代码,请检查

标签: php yii yii2 yii2-advanced-app


【解决方案1】:

你需要使用

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

【讨论】:

  • 为什么?我使用 Yii::$app->user->id 没有任何错误,但现在我有错误?为什么要更改为数组?我需要解决这个问题。
【解决方案2】:

PHP 版本的这个问题。

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

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

【讨论】:

  • PHP 7.2 存在问题
猜你喜欢
  • 2014-03-14
  • 2015-02-26
  • 2012-07-15
  • 2017-12-02
  • 1970-01-01
  • 2013-06-23
  • 2013-08-24
  • 1970-01-01
相关资源
最近更新 更多