gpt4 book ai didi

php - JMS 序列化器不序列化子类

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:33 26 4
gpt4 key购买 nike

我对 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}}}';

那是我的类(class)。家长:

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

最佳答案

您应该检查您是否没有将注释定义的序列化程序与 .yml 文件混合使用。这通常很难调试麻烦。

关于php - JMS 序列化器不序列化子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41427958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com