2013-08-04 60 views
0

我一直在使用「jms/serializer」:「0.13.*@dev」來序列化我的對象。序列化持久性收集與JMS序列化

我在Zend Framework(2)和Doctrine項目中使用它。

這是我的代碼:

use JMS\Serializer\SerializerBuilder as SerializerBuilder; 
(....) 

public function getList() { 

    $em = $this->getEntityManager(); 
    $repo = $em->getRepository('MyApp\Entity\Product'); 
    $hydrator = new DoctrineHydrator($em); 

    $data = array(); 
    foreach ($repo->findAll() as $found) { 
     $data[] = $hydrator->extract($found); 
    } 
    $serializer = SerializerBuilder::create()->build(); 
    $jsonContent = $serializer->serialize($data, 'json'); 

    $this->getResponseWithHeader()->setStatusCode(self::OK_200); 


    return new JsonModel($jsonContent); 
} 

但我收到此錯誤:

Resources are not supported in serialized data. Path: MyApp\Entity\FOO -> Doctrine\ORM\PersistentCollection -> MyApp\Entity\Product -> Doctrine\ORM\PersistentCollection -> DoctrineORMModule\Proxy__CG__\MyApp\MyApp\Brand

顯然,你不能序列化持久化集合。

我搜索了一下,找到了this Symfony相關的問題。但是如何在獨立的Serializer庫中解決這個問題?

非常感謝。


編輯

可這有什麼關係JMS註解?我應該使用某些註釋來實現這一功能嗎?

回答

1

我猜這裏的問題是,PersistentCollection可能包含對數據庫連接的引用,因此JMS無法處理資源類型。

如果你只想要然後第一級連載我想你可以在它砍喜歡:

foreach ($repo->findAll() as $found) { 
    // Set these to basically empty arrays 
    $found->setRelation1(new \Doctrine\Common\Collections\ArrayCollection); 
    $found->setRelation2(new \Doctrine\Common\Collections\ArrayCollection); 
    $data[] = $hydrator->extract($found); 
} 

或者你也可以延長JMS Serialize函數只是忽略資源集合,而不是拋出異常。

+0

感謝您的反饋。但它不會以這種方式填充我的相關對象。他們空着...... – sanders