2014-02-27 28 views
2

我有一個實體實現Serializable接口,這樣的事情:會話反序列化不與實體ID工作

class Entity implements \Serializable 
{ 
    /** 
    * @var integer $id 
    */ 
    protected $id; 

    /** 
    * @var string $name 
    */ 
    protected $name; 

    /** 
    * @see \Serializable::serialize() 
    */ 
    public function serialize() 
    { 
     return serialize(array(
      $this->id, 
      $this->name, 
     )); 
    } 

    /** 
    * @see \Serializable::unserialize() 
    */ 
    public function unserialize($serialized) 
    { 
     list (
      $this->id, 
      $this->name 
     ) = unserialize($serialized); 
    } 
} 

在代碼中的某些時候,我得到這個從數據庫中,並將其保存在與會議

// $entity is an instance of Entity 
$this->getRequest()->getSession()->set('entity', $entity); 

然後,如果我立即嘗試與

$entityFromSession = $this->getRequest()->getSession()->get('entity'); 

實體來獲得實體回是一個不同的類和ID爲空,但name屬性正常工作:

get_class($entityFromSession); // returns 'Proxies\__CG__\Bundle\Entity\Entity' 
$entityFromSession->getId(); // returns null 
$entityFromSession->getNome(); // returns the property correctly. 

編輯:這裏是我所得到的,當我做\主義\ COMMON \的Util \調試::轉儲($實體):

object(stdClass)[779] 
    public '__CLASS__' => string 'Proxies\__CG__\Bundle\Entity\Entity' (length=35) 
    public '__IS_PROXY__' => boolean true 
    public '__PROXY_INITIALIZED__' => boolean false 
    public 'id' => int 44 
    public 'name' => string 'Entity Name' (length=11) 

什麼? id信息在那裏(44是數據庫中的對象的id,這對我沒有意義

回答

1

當你從會話存儲中檢索對象時,你已經分離了實體,你需要調用:

​​
+0

太好了,我知道這會很簡單,謝謝。 – Heitor

相關問題