I am trying to deserialize an entity with a relation using the symfony serializer component. This is my essence:
namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Document { private $id; private $genre; private $name;
And object Genre :
namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; class Genre { private $id; private $name; private $documents; public function __construct() { $this->documents= new ArrayCollection(); }
In my controller action I'm trying to do this now:
$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 following error :
Expected argument of type "AppBundle \ Entity \ Genre", "array" (500) Internal server error)
Is it possible to deserialize a json request with an entity with relationships inside?
Thanks at advace.
json symfony deserialization doctrine2
dacuna
source share