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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…