2017-01-02 28 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'] 
    … 

您可以有两个以上的水平,它的工作原理。 希望这将帮助你......