使用 JMS Serializer 包添加额外的字段 [英] Add extra fields using JMS Serializer bundle

查看:25
本文介绍了使用 JMS Serializer 包添加额外的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通常使用 JMS Serializer 包进行序列化的实体.我必须向序列化添加一些不存在于实体本身但通过一些数据库查询收集的字段.

我的想法是创建一个自定义对象,用实体字段填充字段并添加自定义对象.但这对于类的每个变体(我使用了很多序列化组)来说似乎有点棘手和昂贵.

有没有更好/标准的方法来做到这一点?使用工厂?序列化前/序列化后事件?

也许我可以监听序列化并检查实体类型和序列化组添加自定义字段?但与其对每个实体进行查询,不如收集相关实体的所有数据,然后将其添加到它们中.任何帮助表示赞赏

解决方案

我自己找到了解决方案,

要在序列化完成后添加自定义字段,我们必须创建一个如下所示的侦听器类:

 'serializer.post_serialize', 'class' => 'Acme\DemoBundle\Entity\Team', 'method' => 'onPostSerialize'),);}公共函数 onPostSerialize(ObjectEvent $event){$event->getVisitor()->addData('someKey','someValue');}}

这样您就可以向序列化元素添加数据.

相反,如果您想在序列化之前使用 pre_serialize 事件编辑对象,请注意,如果您想使用 pre_serialize 添加值,您需要已经有一个变量(和正确的序列化组).

I've an entity I usually serialize using the JMS Serializer bundle. I have to add to the serialization some fields that doesn't reside in the entity itself but are gathered with some db queries.

My idea was to create a custom object, fill the fields with the entity fields and add the custom one. But this seems a bit tricky and expensive to do for every variation (I use lot of serialization groups) of the class.

Is there a better/standard way to do this? Using a factory? Pre/Post serialization events?

Maybe I can listen for the serialization and checking entity type and serialization groups add the custom fields? But instead of making a query for each entity it would be better to gather all the data of the related entities and then add it to them. Any help is appreciated

解决方案

I've found the solution by myself,

to add a custom field after the serialization has been done we've to create a listener class like this:

<?php

namespace Acme\DemoBundle\Listener;

use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Acme\DemoBundle\Entity\Team;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\JsonSerializationVisitor;

/**
 * Add data after serialization
 *
 * @Service("acme.listener.serializationlistener")
 * @Tag("jms_serializer.event_subscriber")
 */
class SerializationListener implements EventSubscriberInterface
{

    /**
     * @inheritdoc
     */
    static public function getSubscribedEvents()
    {
        return array(
            array('event' => 'serializer.post_serialize', 'class' => 'Acme\DemoBundle\Entity\Team', 'method' => 'onPostSerialize'),
        );
    }

    public function onPostSerialize(ObjectEvent $event)
    {
        $event->getVisitor()->addData('someKey','someValue');
    }
}

That way you can add data to the serialized element.

Instead, if you want to edit an object just before serialization use the pre_serialize event, be aware that you need to already have a variable (and the correct serialization groups) if you want to use pre_serialize for adding a value.

这篇关于使用 JMS Serializer 包添加额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆