Json JMSSerializer:使用id从数据库检索相关对象
我使用symfony3编写了一个实体,该实体应该通过JSON/JMSSerializer(以RESTful方式)公开。看起来是这样的:Json JMSSerializer:使用id从数据库检索相关对象,json,doctrine-orm,symfony,jmsserializerbundle,Json,Doctrine Orm,Symfony,Jmsserializerbundle,我使用symfony3编写了一个实体,该实体应该通过JSON/JMSSerializer(以RESTful方式)公开。看起来是这样的: /** * MainEntity * * @ORM\Table(name="MainEntity") * @ORM\Entity * */ class MainEntity{ /** * @var integer * * @ORM\Column(name="id", type="integer", nullable
/**
* MainEntity
*
* @ORM\Table(name="MainEntity")
* @ORM\Entity
*
*/
class MainEntity{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
//... some more "simple" fields ...
/**
* @ORM\ManyToOne(targetEntity="SubEntity")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="subentity", referencedColumnName="id")
* })
* @JMS\Accessor(getter="getSubEntityId",setter="setSubEntity")
* @JMS\Type("integer")
* @JMS\SerializedName("subEntityId")
*/
private $subEntity;
//...
public function getSubEntityId() {
return $this->subEntity->getId();
}
}
整个事情的JSON序列化就像一个符咒!特别是,只显示子实体ID,而不是整个子实体,整个子实体可能相当大
因此,不是:
{"id": 1, ..., "subEntity": {"id": 123, "name": "Great subEntity", ...} }
我明白了
{"id": 1, ..., "subEntityId": 123 }
这正是我所需要的
但是当涉及到反序列化时,我会遇到麻烦。。。当然,我也希望在传入请求上使用缩短的JSON格式,但这失败了,因为setSubEntity
需要一个SubEntity
实例,而不是一个数字
在给定ID时,是否有任何方法可以实现反序列化以检索相关对象
我想到了这些可能性:
@JMS\Accessor
注释中给出),获取id并用从数据库检索的对象填充子实体
字段。但这意味着将EntityManager注入实体(或类似的、糟糕的东西…)subEntityId
,并用一个特殊的setter填充它(如上所述)。然后使用控制器读取它,从数据库中获取子实体
对象,并在实体反序列化后使用setSubEntity
方法。对我来说也没那么好非常感谢 将其添加到您的服务配置中,我在这里用YAML编写它
jms_serializer.object_constructor:
alias: jms_serializer.initialized_object_constructor
public: false
jms_serializer.initialized_object_constructor:
class: MyApp\Bundle\CoreBundle\Serializer\InitializedObjectConstructor
arguments: ["@jms_serializer.unserialize_object_constructor"]
为jms\u序列化程序添加此类。已初始化的\u对象\u构造函数
<?php
namespace MyApp\Bundle\CoreBundle\Serializer;
use JMS\Serializer\VisitorInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Construction\ObjectConstructorInterface;
class InitializedObjectConstructor implements ObjectConstructorInterface
{
private $fallbackConstructor;
public function __construct(ObjectConstructorInterface $fallbackConstructor)
{
$this->fallbackConstructor = $fallbackConstructor;
}
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
{
if ($context->attributes->containsKey('target') && $context->getDepth() === 1) {
return $context->attributes->get('target')->get();
}
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}
}
实际上,您不需要创建额外的字段或setter。可能的方法是创建自己的并在映射中使用它
我已经回答了,您可以在那里获得代码示例。非常有用!非常感谢。
protected function flushRequestData(Request $request, $entity = null)
{
$data = $request->getContent();
$dm = $this->get('doctrine_mongodb.odm.default_document_manager');
$context = new DeserializationContext();
if ($entity) {
$context->attributes->set('target', $entity);
}
$deserializedObj = $this->get('serializer')->deserialize(
$data,
$this->getRepository()->getClassName(),
'json',
$context
);
// After deserialized into your entity you need to manually set each of the related entity one by one manually
}