繁体   English   中英

使用symfony2和doctrine获取“无法刷新用户”错误

[英]Getting “cannot refresh user” error with symfony2 and doctrine

我将类User作为基类,然后我从用户类扩展了Teacher。

当我尝试登录时,我收到此错误

您无法从不包含标识符的EntityUserProvider刷新用户。 必须使用Doctrine映射的自己的标识符序列化用户对象。

我在user.php中有序列化/反序列化函数,因为我已经从FOSUserbundle处理了这个函数

public function serialize()
    {
        return serialize(array(
            $this->password,
            $this->salt,
            $this->usernameCanonical,
            $this->username,
            $this->expired,
            $this->locked,
            $this->credentialsExpired,
            $this->enabled,
        ));
    }

我无法找到我可以检查错误的位置。 我卡住了。 请帮忙

"You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine."

这不是问题的答案,但如果有人谷歌搜索上面的错误信息(就像我做的那样),这是最受欢迎的。 自从我找到解决方案后,我想我会分享。

如果从数据库中删除当前用户,然后尝试重定向用户,则会出现此错误。 (这似乎是一个愚蠢的错误,但错误信息肯定没有帮助!)解决方案是在重定向之前简单地清除会话。

$this->get('security.context')->setToken(null);
$this->get('request')->getSession()->invalidate();

EntityUserProvider.php代码看来,您还必须序列化用户的id

刚使用类表继承有同样的问题,它是由id属性设置为private的可见性引起的,因此子类无法访问id,将此更改为protected解决了它。

User对象在某些时候被克隆,所以一定要这样做(就像我一样):

function __clone()
{
    $this->id = null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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