反序列化与Symfony Serializer组件关系的实体 [英] Deserialize an entity with a relationship with Symfony Serializer Component

查看:128
本文介绍了反序列化与Symfony Serializer组件关系的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用symfony序列化器组件对具有关系的实体进行反序列化。这是我的实体:

 命名空间AppBundle\Entity; 

使用Doctrine\ORM\Mapping作为ORM;

/ **
*文件
*
* @ ORM\Table(name =document)
* @ ORM\Entity repositoryClass =AppBundle\Repository\DocumentRepository)
* /
class文档
{
/ **
* @var int
*
* @ ORM\Column(name =id,type =integer)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* /
private $ id;

/ **
* @ ORM\ManyToOne(targetEntity =Genre,inversedBy =documents)
* @ ORM\JoinColumn(name =id_genre ,referencedColumnName =id)
* /
private $ genre;

/ **
* @var string
*
* @ ORM\Column(name =name,type =string,length = 100 )
* /
private $ name;

//这里的getter和setter
...
}

流派实体

 命名空间AppBundle\Entity; 

使用Doctrine\ORM\Mapping作为ORM;
使用Doctrine\Common\Collections\ArrayCollection;

/ **
*流派
*
* @ ORM\Table(name =genre)
* @ ORM\Entity repositoryClass =AppBundle\Repository\GenreRepository)
* /
class类型
{
/ **
* @var int
*
* @ ORM\Column(name =id,type =integer)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* /
private $ id;

/ **
* @var string
*
* @ ORM\Column(name =name,type =string,length = 50 ,nullable = true)
* /
private $ name;

/ **
* @ ORM\OneToMany(targetEntity =Document,mappedBy =genre)
* /
private $ documents;

public function __construct()
{
$ this-> documents = new ArrayCollection();
}

//这里的getter和setter
....
}

在我的控制器操作中,我正在尝试:

 code> $ encoders = array(new JsonEncoder()); 
$ normalizers = array(new ObjectNormalizer());
$ serializer = new Serializer($ normalizers,$编码器);

$ document = $ serializer-> deserialize($ request-> getContent(),'AppBundle\Entity\Document','json');

我的 json数据

  {name:我的文档,类型:{id:1,name:我的流派}} 

但是,我收到了下一个错误


类型为AppBundle\Entity\Genre,array的预期参数(500
内部服务器错误)




是否可以使用与内部关系的实体反序列化一个json请求?



感谢advace。

解决方案

是和否首先,您不应该在控制器中重新创建序列化程序的新实例,而是使用 serializer 服务。



第二,Symfony序列化程序不可能开箱即用。我们正在 https://api-platform.com/ 中进行操作,但这里有一些魔法。也就是说,公关已经被支持: https://github.com/symfony/symfony / pull / 19277


I'm trying to deserialize an entity with a relationship using the symfony serializer component. This is my entity:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Document
 *
 * @ORM\Table(name="document")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DocumentRepository")
 */
class Document
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Genre", inversedBy="documents")
     * @ORM\JoinColumn(name="id_genre", referencedColumnName="id")
     */
    private $genre;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;

    //getters and setters down here
    ...
}

And the Genre entity:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Genre
 *
 * @ORM\Table(name="genre")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\GenreRepository")
 */
class Genre
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50, nullable=true)
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="Document", mappedBy="genre")
     */
    private $documents;

    public function __construct()
    {
        $this->documents= new ArrayCollection();
    }

    //getters and setters down here
    ....
}

In my controller action right now I'm trying this:

$encoders = array(new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);

$document = $serializer->deserialize($request->getContent(), 'AppBundle\Entity\Document', 'json');

And my json data:

{"name": "My document", "genre": {"id": 1, "name": "My genre"}}

But I got the next error:

Expected argument of type "AppBundle\Entity\Genre", "array" given (500 Internal Server Error)

Is possible to deserialize a json request with an entity with relations inside?

Thanks in advace.

解决方案

Yes and no. First, you shouldn't re-create a new instance of the serializer in your controller but use the serializer service instead.

Second, no it's not possible out of the box with Symfony serializer. We are doing it in https://api-platform.com/ but there is a bit of magic there. That said, a PR has been made to support it: https://github.com/symfony/symfony/pull/19277

这篇关于反序列化与Symfony Serializer组件关系的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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