Disable an entity with a relation to the Symfony Serializer component

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; /** * 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 object Genre :

 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 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.

+8
json symfony deserialization doctrine2
source share
3 answers

Yes and no. First, you should not re-create a new instance of the serializer in your controller, but use the serializer instead.

Secondly, this is not possible out of the box with the Symfony serializer. We do this at https://api-platform.com/ , but there is a bit of magic there. However, a PR was made to support it: https://github.com/symfony/symfony/pull/19277

+5
source share

Now it works. You need to include property_info in config.yml:

  framework: property_info: enabled: true 
+1
source share

This is what Symfony documentation calls β€œ Recursive Denormalization,” from version 3.3 to the actual wizard, 4.0.

For Symfony to detect the property types of serialized objects, it needs to use the PropertyInfo component, which, as indicated by its @ slk500 answer, must be activated in the wireframe configuration .

So, if you use the full framework, all you need to do to deserialize the nested json objects is:

1.Find out the serializer and components of the property information in the config.yml file:

 framework: #... serializer: { enabled: true } property_info: { enabled: true } 
  1. Then enter the serializer where you need it:
 <?php // src/AppBundle/Controller/DefaultController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller { public function indexAction(SerializerInterface $serializer, Request $request) { $document = $serializer->deserialize($request->getContent(), 'AppBundle\Entity\Document', 'json'); // ... } } 

The standard features of these components were sufficient for my needs.
Autowiring takes care of the main service declaration, so if you don’t need certain normalizers, you don’t even need to edit the services.yml configuration file. Depending on your use cases, you may need to enable certain features. Check out the Serializer and PropertyInfo documentation for (hopefully) more specific use cases.

0
source share

All Articles