JMS Serializer没有序列化子类

时间:2017-01-02 13:41:39

标签: php symfony jmsserializerbundle symfony-2.8 jms-serializer

我遇到了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;

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在儿童班中尝试改变

@JMS\ExclusionPolicy("all")

OpenGL vender string: Intel
OpenGL renderer string : Intel(R) HD Graphics 4600
OpenGL core profile version string: 1.2 (4.2.0 - Build 10.18.10.4226)
OpenGL core profile extensions:
OpenGL version string: 1.2 (4.2.0 - Build 10.18.10.4226)
OpenGL extensions:

“无”正在弄乱@Groups

答案 2 :(得分:-1)

我认为你应该使用这个注释:“@discriminator” 所以读这个! https://jmsyst.com/libs/serializer/master/reference/annotations#discriminator 我有同样的问题,发现我的问题:) (对不起,我使用YML unstead注释,但工作是一样的) 在我最低级别的abstact实体中,这里是带有field_name“class_name”的鉴别器(并在属性中定义!): (yml文件:Entity.Bases.Basentity.yml - 不要忘记“.Bases”或您的Entity目录中的其他子目录!)

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']
    …

你可以拥有两个以上的关卡,它有效。 希望能帮到你...