得到";无法刷新用户“;符号2和学说的错误


Getting "cannot refresh user" error with symfony2 and doctrine

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

当我尝试登录时,我得到了这个错误

不能从EntityUserProvider刷新没有包含标识符。用户对象必须与其由条令映射的自己的标识符。

我在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解决了这个问题。

用户对象在某个时刻被克隆,所以请确保不要这样做(就像我所做的那样):

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