JMS Serializer does not serialize child classes

I have a problem with a JMS serializer. When I use groups, JMS does not serialize my child classes, but when I do not use groups, everything is fine. What am I doing wrong?

$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); 

Give:

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

And these are my classes. Parent:

 /** * 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; 

And the child class:

 /** * 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; 
+5
source share
3 answers

You should check to see if the serializer defined by annotations is mixed with .yml files. This often makes debugging difficult.

+1
source

in the child's classroom try changing

 @JMS\ExclusionPolicy("none") 

to

 @JMS\ExclusionPolicy("all") 

"none" messing around with @Groups

0
source

I think you should use this annotation: "@discriminator" So read this! https://jmsyst.com/libs/serializer/master/reference/annotations#discriminator I had the same problem and I found my problem :) (Sorry, I use YML annotations, but the job is the same) In my younger abstraction of an object there is a discriminator with the field_name "class_name" (and defined in the properties!): (file yml: Entity.Bases.Basentity.yml - do not forget ".Bases" or other subdirectories in the Entity directory!)

 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 

Now in the definition of my object (extends my Basentity.php) you can add its own fields. I just discovered this, so I can not say more ...

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

You can have more than two levels, it works. Hope this helps you ...

-1
source

Source: https://habr.com/ru/post/1262177/


All Articles