简体   繁体   中英

Getting “cannot refresh user” error with symfony2 and doctrine

I have the class User as the base class then i extended Teacher from user class.

When i try to login i get this error

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.

I have serialize/deserialize function in user.php as i have coped that from FOSUserbundle

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

I am not able to find where i can check for error. i am stuck. please help

"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 is not the answer to the question, but if someone googles the error message above (like I did) this is the top hit. And since I found my solution, I thought I'd share.

You will get this error if you remove the current user from the database and then try to redirect the user. (Which might seem like a silly mistake, but the error message sure isn't helping!) The solution is to simply clear out the session before you redirect.

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

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

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

The User object get's cloned at some point, so be sure to not do this (as I did):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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