Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

json - Deserialize an entity with a relationship with Symfony Serializer Component

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

namespace AppBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * Document
 *
 * @ORMTable(name="document")
 * @ORMEntity(repositoryClass="AppBundleRepositoryDocumentRepository")
 */
class Document
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="Genre", inversedBy="documents")
     * @ORMJoinColumn(name="id_genre", referencedColumnName="id")
     */
    private $genre;

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

    //getters and setters down here
    ...
}

And the Genre entity:

namespace AppBundleEntity;

use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;

/**
 * Genre
 *
 * @ORMTable(name="genre")
 * @ORMEntity(repositoryClass="AppBundleRepositoryGenreRepository")
 */
class Genre
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORMOneToMany(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(), 'AppBundleEntityDocument', 'json');

And my json data:

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

But I got the next error:

Expected argument of type "AppBundleEntityGenre", "array" given (500 Internal Server Error)

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

Thanks in advace.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...