JMSSerializer Deserializing One-to-One Doctrine Problem

I have two Doctrine objects connected in a one-to-one relationship. I am trying to deserialize the json object returned from the request, but JMS does not set the back of the association. I was at this hour without a decision.

The code is below.

<?php namespace MyNamspace\Entity; //Annotations namespaces use JMS\SerializerBundle\Annotation as JMS; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="MyNamspace\Repository\Policy") * @ORM\Table(name="policies") * @JMS\ExclusionPolicy("all") */ class Policy { /** * [$id description] * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") * @JMS\Expose * @JMS\AccessType("public_method") */ protected $id; /** * [$name description] * @var string * @ORM\Column(type="string", length=360) * @JMS\Expose * @JMS\AccessType("public_method") * @JMS\Type("string") */ protected $name; /** * [$policydata description] * * @ORM\OneToOne(targetEntity="PolicyData",mappedBy="policy",cascade={"persist","remove"}) * @JMS\Type("MyNamspace\Entity\PolicyData") * @JMS\AccessType("public_method") * @JMS\Expose */ protected $policydata; public function setId($id) { $this->id = $id; return $this; } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; return $this; } public function getName() { return $this->name; } public function setPolicyData(\MyNamspace\Entity\PolicyData $policydata) { $this->policydata = $policydata; return $this; } public function getPolicyData(){ return $this->policydata; } 

The second object is below;

 <?php namespace MyNamspace\Entity; //Annotations namespaces use JMS\SerializerBundle\Annotation as JMS; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="MyNamspace\Repository\PolicyData") * @ORM\Table(name="policies_data") * @JMS\ExclusionPolicy("all") */ class PolicyData { /** * [$id description] * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") * @JMS\Expose * @JMS\AccessType("public_method") */ protected $id; /** * [$effect description] * @var integer * @ORM\Column(type="integer") * @JMS\Expose * @JMS\AccessType("public_method") * @JMS\Type("integer") * */ protected $effect; /** * @ORM\OneToOne(targetEntity="Policy",inversedBy="policydata") * @ORM\JoinColumn(name="policy_id", referencedColumnName="id") * @JMS\AccessType("public_method") * @JMS\Type("MyNamspace\Entity\Policy") * @JMS\Expose */ protected $policy; /** * [$startdate description] * @var datetime * @ORM\Column(type="datetime",nullable=true) * @JMS\Expose * @JMS\AccessType("public_method") * @JMS\Type("DateTime") * */ protected $startdate; public function setId($id) { $this->id = $id; return $this; } public function getId() { return $this->id; } public function getEffect() { return $this->effect; } public function setEffect($effect) { $this->effect = $effect; return $this; } public function setPolicy(\MyNamspace\Entity\Policy $policy) { $this->policy = $policy; } public function getPolicy() { return $this->policy; } public function getStartDate() { return $this->startdate; } public function setStartDate($startdate) { $this->startdate = $startdate; return $this; } 

I am using Silex, and controller extraction code:

 public function connect(Application $app) { //New controller from the factory $controller = $app['controllers_factory']; //Create a policy (either empty or complete or maybe halfway between the 2) $controller->post('/', function(Request $request) use ($app){ // Need to intercept the request to format the dates or else JMS will not deserialize if($request->isMethod('POST')) { $jsonData = $request->getContent(); $dateHandler = new DateHandler(); $data = $dateHandler->formatDate($jsonData); } print_r($data); $policy = $app['serializer']->deserialize($data, 'Verloc\Entity\Policy', 'json'); print_r($policy); die; $policy = $app['service.rights']->addPolicy($policy); return new Response( $app['serializer']->serialize($policy, 'json'), 200 ); }); 

Assuming serializable data:

 { "name": "Policy1", "policydata": [ { "effect": "1", "startdate": "2012-10-21T00:00:00+0100" } ] } 

What I get after serialization is:

 MyNamspace\Entity\Policy Object ( [id:protected] => [name:protected] => Policy1 [policydata:protected] => MyNamspace\Entity\PolicyData Object ( [id:protected] => [effect:protected] => [policy:protected] => [startdate:protected] => ) ) 

You can see that while the main policy object is deserialized, the PolicyData object is empty.

+4
source share

Source: https://habr.com/ru/post/1441636/


All Articles