簡體   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