2017-01-02 121 views
5

我有JMS序列化程序的問題。當我使用組時,JMS不會序列化我的子類,但是當我不使用組時,一切都可以。我做錯了什麼?JMS序列化程序不序列化子類

$context = SerializationContext::create()->enableMaxDepthChecks(); 
    $context->setGroups(['clientapi']); 

    $contextWithoutGroup = SerializationContext::create()->enableMaxDepthChecks(); 

    /** @var Serializer $serializer */ 
    $serializer = $this->container->get('jms_serializer'); 
    $dataClientApi = $serializer->serialize($documentBundle->getFolderDocumentsForClientApi(
     $this->getUserFromParam($params), $folder, $categories, $tags 
    ), 'json', $context); 

    $dataWithout = $serializer->serialize($documentBundle->getFolderDocumentsForClientApi(
     $this->getUserFromParam($params), $folder, $categories, $tags 
    ), 'json', $$contextWithoutGroup); 

給:

$dataClientApi = '{"0":{"author":{}}}'; 
$dataWithout = '{"0":{"author":{id: 2}}}'; 

這是我的課。家長:

/** 
* Document 
* 
* @ORM\Table(name="documents") 
* @ORM\Entity(repositoryClass="AppBundle\Entity\DocumentRepository") 
* @ORM\EntityListeners({"DocumentListener"}) 
* @JMS\ExclusionPolicy("all") 
* @JMS\AccessorOrder("custom", custom = {"id", "folderId", "title"}) 
*/ 
class Document implements ResourceInterface 
{ 

    use Traits\SortableTrait; 
    use Traits\TimestampableTrait; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(type="integer", name="id") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @JMS\Groups({"clientapi"}) 
    * @JMS\Expose() 
    */ 
    protected $id; 

    /** 
    * @var \AppBundle\Entity\Author 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author") 
    * @ORM\JoinColumn(name="author_id", nullable=true, referencedColumnName="id") 
    * @JMS\Groups({"clientapi"}) 
    * @JMS\MaxDepth(3) 
    * @JMS\Expose() 
    */ 
    protected $author; 

而子類:

/** 
* Author 
* 
* @ORM\Entity 
* @ORM\Table(name="author") 
* @Gedmo\Uploadable(pathMethod="getDirPath", allowOverwrite=false, filenameGenerator="SHA1", appendNumber=true) 
* @JMS\ExclusionPolicy("none") 
* @JMS\AccessorOrder("custom", custom = {"id"}) 
*/ 
class Author implements ResourceInterface, FileInterface 
{ 
    const DIR_PATH = 'web/system/authors'; 

    use Traits\FileTrait; 
    use Traits\TimestampableTrait; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    * @JMS\Groups({"clientapi"}) 
    * @JMS\Expose() 
    */ 
    protected $id; 
+0

我不能跟你提供一個乾淨的項目中的代碼重現該問題... – Hammerbot

回答

0

在子類中嘗試改變

@JMS\ExclusionPolicy("none") 

@JMS\ExclusionPolicy("all") 

的 「無」 與@Groups

搞亂
+0

想這一點,而可悲的是改變什麼:/ –

+1

您使用的註釋的緩存或者是你在prod environnement?在你的控制器中有一個錯誤:$$ contextWithoutGroup你在兩個$ –

+0

耶,但是這個錯誤只是在這裏(我改變變量名稱,使樣品更易讀) 和100%不是緩存的事情。我有禁用緩存,我總是也清除緩存等,什麼都不變。 (我確信abotu緩存的事情,因爲我看到「父」類的變化) –

-1

我想你應該使用這個註釋:「@discriminator」 所以閱讀這個! https://jmsyst.com/libs/serializer/master/reference/annotations#discriminator 我有同樣的問題,結果發現我的問題:) (對不起,我用YML unstead註釋,但工作是一樣的) 在我滿級abstact實體,這裏是與FIELD_NAME「CLASS_NAME」鑑別在 - (不要忘了在實體目錄「.Bases」或其他子目錄Entity.Bases.Basentity.yml陽明文件!)

AppBundle\Entity\Bases\Basentity: 
exclusion_policy: ALL 
discriminator: 
    field_name: class_name 
    disabled: false 
    map: 
     Basentity: 'AppBundle\Entity\Bases\Basentity' 
     Item: 'AppBundle\Entity\Bases\Item' 
     Tier: 'AppBundle\Entity\Bases\Tier' 
     Atelier: 'AppBundle\Entity\Atelier' 
     Tva: 'AppBundle\Entity\Tva' 
     Language: 'AppBundle\Entity\Language' 
     User: 'UserBundle\Entity\User' 
properties: 
    id: 
     type: integer 
     … 
    class_name: 
     type: string 
     accessor: 
      getter: getShortName 

現在:(並在屬性定義!) 我的實體定義(擴展我的Basentity.php),你可以添加它自己的字段。 我剛剛發現這一點,所以不能告訴更多...

AppBundle\Entity\Language: 
exclusion_policy: ALL 
properties: 
    name: 
     type: string 
     groups: ['paged', 'user', 'tva', 'language'] 
    slug: 
     type: string 
     groups: ['paged', 'user', 'tva', 'language'] 
    … 

您可以有兩個以上的水平,它的工作原理。 希望這將幫助你......