简体   繁体   English

Symfony 序列化器:反序列化关系

[英]Symfony Serializer: Deserialize with relation

I test the serializer component and try to do the following.我测试序列化程序组件并尝试执行以下操作。

I have an Article entity:我有一个文章实体:

class Article
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="article", orphanRemoval=true, cascade={"persist"})
     */
    private $comments;

//getter/setter...

and a Comment entity:和评论实体:

class Comment
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=50)
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\ManyToOne(targetEntity=Article::class, inversedBy="comments", cascade={ "persist" })
     * @ORM\JoinColumn(nullable=false)
     */
    private $article;

//getter/setter...

When a send a json to the controller:当发送 json 到 controller 时:

{
    "title": "My comment",
    "content": "This is a comment",
    "article": {
        "id": 1
    }
}
  • Article with id 1 exists存在 id 为1的文章

I would like the relationship with Article to be deserialized:我想反序列化与 Article 的关系:

 #[Route('', name: "comment_create", methods: ['POST'])]
    public function create(Request $request): JsonResponse 
    {
        $comment = $this->serializer->deserialize($request->getContent(), Comment::class, 'json');
//        $this->entityManager->persist($comment);
//        $this->entityManager->flush();
        return $this->json($comment, Response::HTTP_CREATED);
    }

but the article is not linked.但文章没有链接。

{"id":null,"title":"My comment","content":"This is a comment","article":{"id":null,"title":null,"content":null,"comments":[]}}

Where is my mistake?我的错误在哪里? Can the symfony serializer do this? symfony 序列化程序可以做到这一点吗?

Try to use following for deserialization:尝试使用以下进行反序列化:

$comment = $this->serializer->deserialize($request->getContent(), 'App\Entity\Comment', 'json');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony 序列化程序无法反序列化为 \\DateTime - Symfony serializer cannot deserialize into \DateTime 如何使用Symfony序列化器反序列化复杂对象的数组? - How to deserialize an array of complex objects with Symfony Serializer? Symfony JMS序列化器将json数组反序列化为一个整数 - Symfony JMS Serializer deserialize array of jsons to an entitie Symfony序列化程序无法在反序列化时处理Doctrine ID - Symfony Serializer cannot handle Doctrine Id on deserialize 如何在Symfony序列化器中序列化实体关系ManyToOne? - How to serialize entity relation ManyToOne in Symfony Serializer? 如何在 Symfony Serializer 中反序列化一组对象? - How can I deserialize an array of objects in Symfony Serializer? symfony 序列化程序可以反序列化返回子实体类型的嵌套实体吗? - Can symfony serializer deserialize return nested entity of type child entity? 使用 Symfony Serializer Component 和 AbstractNormalizer::OBJECT_TO_POPULATE 反序列化实体数组 - Deserialize a entities array with Symfony Serializer Component and AbstractNormalizer::OBJECT_TO_POPULATE Symfony Serializer 组件:反序列化混合类型的对象数组 - Symfony Serializer component: Deserialize array of objects of mixed types 使用 Symfony Serializer 将 JSON 反序列化为类实例数组 - Deserialize JSON into an array of class instances using Symfony Serializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM