JMSSerializerBundle:处理同一实体的不同名称 属性
JMSSerializerBundle: handle different name for same entity property
据我了解,使用组 and/or 版本控制等排除技术,可以序列化或不序列化实体字段,但我想要的设置不同 "serialized_name".
我有一个旧的 ApiBundle,它在第三个包中序列化实体,现在我需要从 NewAPIBundle 序列化相同的实体,但我需要新的字段名称而不更改实体的 属性 名称(旧 api应该继续经营)
是否可以用 JSMSerializer 做这样的事情? :
Entity\User:
properties:
name:
groups: ['user_odlapi']
name:
groups: ['user_newapi']
serialized_name: 'username'
或者,是否可以根据请求编写两个不同的 yml 序列化文件url/Bundle?
您可以按照 here in the doc 所述实现 EventSubscriberInterface。
例如:
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
...
class UserSerializerSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
[
'event' => 'serializer.post_serialize',
'class' => 'Entity\User',
'method' => 'onPostSerialize',
],
];
}
public function onPostSerialize(ObjectEvent $event)
{
$visitor = $event->getVisitor();
$user = $event->getObject();
$attributeName = 'name'; // default
if ($this->isInGroups(['user_newapi'], $event))
{
$attributeName = 'username';
}
$visitor->addData($attributeName, $user->getName());
}
private function isInGroups(array $validGroup, ObjectEvent $event)
{
$option = $event->getContext()
->attributes->get('groups')
->filter(function ($groups) use ($validGroup) {
return sizeof(array_intersect($groups, $validGroup))>0;
});
return !$option->isEmpty();
}
并注册,如YML示例:
acme.user_serializer_subscriber:
class: Acme\DemoBundle\Subscriber\UserSerializerSubscriber
tags:
- { name: "jms_serializer.event_subscriber" }
希望对您有所帮助
据我了解,使用组 and/or 版本控制等排除技术,可以序列化或不序列化实体字段,但我想要的设置不同 "serialized_name".
我有一个旧的 ApiBundle,它在第三个包中序列化实体,现在我需要从 NewAPIBundle 序列化相同的实体,但我需要新的字段名称而不更改实体的 属性 名称(旧 api应该继续经营)
是否可以用 JSMSerializer 做这样的事情? :
Entity\User:
properties:
name:
groups: ['user_odlapi']
name:
groups: ['user_newapi']
serialized_name: 'username'
或者,是否可以根据请求编写两个不同的 yml 序列化文件url/Bundle?
您可以按照 here in the doc 所述实现 EventSubscriberInterface。
例如:
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
...
class UserSerializerSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
[
'event' => 'serializer.post_serialize',
'class' => 'Entity\User',
'method' => 'onPostSerialize',
],
];
}
public function onPostSerialize(ObjectEvent $event)
{
$visitor = $event->getVisitor();
$user = $event->getObject();
$attributeName = 'name'; // default
if ($this->isInGroups(['user_newapi'], $event))
{
$attributeName = 'username';
}
$visitor->addData($attributeName, $user->getName());
}
private function isInGroups(array $validGroup, ObjectEvent $event)
{
$option = $event->getContext()
->attributes->get('groups')
->filter(function ($groups) use ($validGroup) {
return sizeof(array_intersect($groups, $validGroup))>0;
});
return !$option->isEmpty();
}
并注册,如YML示例:
acme.user_serializer_subscriber:
class: Acme\DemoBundle\Subscriber\UserSerializerSubscriber
tags:
- { name: "jms_serializer.event_subscriber" }
希望对您有所帮助