JMS 序列化程序未序列化 child 类
JMS Serializer not serializing child classes
我对 JMS 序列化程序有疑问。当我使用组时,JMS 不会序列化我的 child classes,但是当我不使用组时,一切都很好。我做错了什么?
$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}}}';
那是我的 class。 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;
和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;
在 child class 尝试更改
@JMS\ExclusionPolicy("none")
至
@JMS\ExclusionPolicy("all")
"none" 与@Groups
发生冲突
您应该检查您是否没有将注释定义的序列化程序与 .yml 文件混合使用。经常会让人难以调试的烦恼。
我对 JMS 序列化程序有疑问。当我使用组时,JMS 不会序列化我的 child classes,但是当我不使用组时,一切都很好。我做错了什么?
$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}}}';
那是我的 class。 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;
和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;
在 child class 尝试更改
@JMS\ExclusionPolicy("none")
至
@JMS\ExclusionPolicy("all")
"none" 与@Groups
发生冲突您应该检查您是否没有将注释定义的序列化程序与 .yml 文件混合使用。经常会让人难以调试的烦恼。