我有两个实体<代码>书籍和作者
class Book
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=255)
*/
private string $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Author", fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="books_authors",
* joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
* )
* @var PersistentCollection|ArrayCollection
*/
private $authors;
public function __construct()
{
$this->authors = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getAuthors()
{
return $this->authors;
}
public function setAuthors($authors)
{
$this->authors = $authors;
}
public function addAuthor($author){
if(!$this->authors->contains($author))
$this->authors->add($author);
}
public function removeAuthor($author){
if($this->authors->contains($author)){
$this->authors->removeElement($author);
}
}
}
在BookController中,我尝试创建新书并使用序列化器书体,如
{
"name:"book name",
"authors":[
1,
2
]
}
In控制器
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
$book = $this->serializer->deserialize($request->getContent(), Book::class, 'json');
try{
$this->entityManager->persist($book);
$this->entityManager->flush();
} catch (UniqueConstraintViolationException $exception){
return new JsonResponse('already exist', JsonResponse::HTTP_FOUND);
}
但是在flush()上我有例外:
关联字段“App\Entity\Book#$authors”的类型为“App\Entity\Author”的预期值改为“integer”。
因为<代码>书-
我有一些解决这个问题的变体,但是创建和更新实体的最佳方式是什么?
你需要像这样创建EntityNormalizer:[中后][1]
class EntityNormalizer extends ObjectNormalizer implements DenormalizableInterface
{
private $em;
public function __construct(EntityManagerInterface $em, ?ClassMetadataFactoryInterface $classMetadataFactory = null, ?NameConverterInterface $nameConverter = null, ?PropertyAccessorInterface $propertyAccessor = null, ?PropertyTypeExtractorInterface $propertyTypeExtractor = null)
{
$this->em = $em;
parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor);
}
public function supportsNormalization($data, $format = null)
{
return false;
}
public function supportsDenormalization($data, $type, $format = null)
{
return !$this->em->getMetadataFactory()->isTransient($type) && (is_string($data) || is_numeric($data));
}
public function denormalize($data, $class, $format = null, array $context = [])
{
return $this->em->find($class, $data);
}
}
[1]: https://medium.com/cloudstek/using-the-symfony-serializer-with-doctrine-relations-69ecb17e6ebd
I'va是一个OID接口,可以由许多具体类型实现: 现在我有一个具有两个字段的对象,一个使用抽象接口类型(OID)定义,另一个使用具体类型(MyOID)定义 我想使用jackson以不同的方式序列化/反序列化字段,无论它们是使用抽象接口类型还是具体类型定义的: 注意,被序列化,包括类型信息(多态序列化),而被序列化为文本 为此,我将OID接口注释为: 并为每个具体类型分配了类型id: 最后,对容器
问题内容: 我正在使用JAVA 1.6和Jackson 1.9.9我有一个枚举 我添加了一个@JsonValue,这似乎可以将对象序列化为: 但是当我尝试反序列化时,我得到了 我在这里想念什么? 问题答案: 如果你希望将枚举类与其JSON表示完全脱钩,则@xbakesx指出的序列化器/反序列化器解决方案是一个很好的解决方案。 另外,如果你喜欢一个独立的解决方案,则基于·和·注释的实现会更方便。 因
问题内容: 我正在尝试使用symfony序列化程序组件反序列化具有关系的实体。这是我的实体: 和 流派实体 : 现在在我的 控制器操作中, 我正在尝试以下操作: 还有我的 json数据 : 但是我得到了下一个 错误 : 给定类型为“ AppBundle \ Entity \ Genre”,“ array”的预期参数(500内部服务器错误) 是否可以使用内部具有关系的实体反序列化json请求? 感谢
客户端有 3 种序列化器可用。你可能永远都不会更改序列化器,除非你有特殊需求或者要实现一个新的协议。 序列化器的工作是 encode 发送的请求体和 decode 返回的响应体。在 99% 的例子中,这就是一种简单转换为JSON数据或解析 JSON 数据的工具。 默认的序列化器是 SmartSerializer 。 SmartSerializer Serialize() SmartSerializ
问题内容: 我注意到存储在Redis中的某些序列化对象在反序列化方面遇到问题。 当我对Redis中存储的对象类进行更改时,通常会发生这种情况。 我想了解问题,以便为解决方案设计一个清晰的方案。 我的问题是,什么导致反序列化问题?移除公共/私人财产会引起问题吗?也许添加新属性?向类添加新功能会产生问题吗?那么更多的构造函数呢? 在我的序列化对象中,我有一个属性Map,如果我更改(更新了一些属性,添加
问题内容: Golang中将结构序列化和反序列化为字符串的最佳方法(完整性和性能)是什么,反之亦然? 例如,如果我有这个结构: 我想将其存储在Redis上并取回。我试过保存,整型和字符串,这很好,但是如何存储结构对象? 问题答案: 使用gob和base64可以解决问题,例如: 当您需要序列化自定义结构或类型(例如struct)时,只需添加以下行: