JMS serializer - Why new objects are not created through the constructor

Why new entities created with a zero value for all values, except for the data in json, why the constructor of the object does not set default values ​​- setting die () in the constructor is never performed.

Update:

So, transcode the code when the managed object is not found, JMSS will use the doctinary doctrine class to create the object - its only task is to create objects without calling the constructor. Is there a reason for this? this is inside JMS\Serializer\Construction\UnserializeObjectConstructor


I configured the object constructor to use the doctrine object constructor written by JMS, but the same problem occurs without it.

 jms_serializer.object_constructor: alias: jms_serializer.doctrine_object_constructor public: false 

Existing objects are updated without problems, but new entities lack all the default values ​​for the constructor.

In the element "fields" 0 there is an element 1, new.

 array (size=3) 'id' => int 2 'name' => string 'Categories' (length=10) 'fields' => array (size=2) 0 => array (size=7) 'id' => int 49 'displayName' => string 'Car Branded' (length=11) 'type' => string 'checkboxlist' (length=12) 'required' => boolean false 'disabled' => boolean false 'name' => string 'h49' (length=3) 1 => array (size=3) 'type' => string 'email' (length=5) 'name' => string 'field3491' (length=9) 'displayName' => string 'Email' (length=5) 

The entity is as follows after deserialization:

 object(stdClass)[2000] public '__CLASS__' => string 'AppBundle\Entity\FormElement' (length=28) public 'id' => null public 'label' => string 'Email' (length=5) public 'type' => string 'email' (length=5) public 'defaultValue' => null public 'required' => null public 'mappedField' => null public 'garbageCollection' => null public 'sortOrder' => null public 'disabled' => null public 'uuid' => null public 'form' => null public 'multiOptions' => null public 'filters' => null public 'submissions' => null 

Object constructor:

 public function __construct() { $this->required = false; $this->disabled = false; $this->garbageCollection = false; $this->sortOrder = 0; $this->type = 'text'; } 

And finally, this is how im deserializing:

 $serializer = $this->get('jms_serializer'); $entryForm = $serializer->deserialize($json_data, 'AppBundle\Entity\EntryForm', 'json'); 
+5
source share
1 answer

The problem is that the ObjectConstructor uses the Doctrine Instantiator by default, which does not call the class constructor. To solve this problem, you can create your own ObjectConstructor, which simply returns a new instance of the class.

Example:

 <?php namespace AppBundle\Serializer; use JMS\Serializer\Construction\ObjectConstructorInterface; use JMS\Serializer\DeserializationContext; use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\VisitorInterface; class ObjectConstructor implements ObjectConstructorInterface { /** * {@inheritdoc} */ public function construct( VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context ) { $className = $metadata->name; return new $className(); } } 

If you are using a package, simply set this new class for the jms_serializer.unserialize_object_constructor.class parameter. Otherwise, in your builder, use the class as your object constructor.

+7
source

All Articles