4115

Cannot create an instance of DateTime from serialized data

I have a problem with the date. Maybe I don't understand something in the process of serialization, but, when, I send a datetime to Api-Platform (Symfony) for example

dateEvaluation: "2017-12-05T11:52:00.000Z"

I obtain this message of error

Cannot create an instance of DateTime from serialized data because its constructor requires parameter "time" to be present.

This is my Entity

<?php namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; /** * @ORM\Entity(repositoryClass="App\Repository\EvaluationRepository") * @ORM\HasLifecycleCallbacks() * @ApiResource(attributes={ * "normalization_context"={ * "groups"={"Evaluation"} * } * }) */ class Evaluation { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") * @Groups({"NotesEvaluations", "Evaluation"}) */ private $id; /** * @var \DateTime * @ORM\Column(type="datetime") * @Groups({"Evaluation"}) */ private $created; /** * @var \DateTime * @ORM\Column(type="datetime") * @Groups({"Evaluation"}) */ private $updated; /** * @var \DateTime * @ORM\Column(type="datetime") * @Groups({"Evaluation"}) */ private $dateEvaluation; /** * @var string * * @ORM\Column(type="text") * @Groups({"Evaluation"}) */ private $commentaire; /** * @var * * @ORM\ManyToOne(targetEntity="App\Entity\Personnel") * @Groups({"NotesEvaluations", "Evaluation"}) */ private $auteur; /** * @var * * @ORM\ManyToMany(targetEntity="App\Entity\Personnel") */ private $autorises; /** * @var integer * * @ORM\Column(type="integer") */ private $annee; /** * @var * * @ORM\ManyToOne(targetEntity="App\Entity\Module", inversedBy="evaluations") * @Groups({"Evaluation"}) */ private $module; /** * @var Boolean * * @ORM\Column(type="boolean") * @Groups({"NotesEvaluations"}) */ private $visible; /** * @var Boolean * * @ORM\Column(type="boolean") * @Groups({"NotesEvaluations"}) */ private $modifiable; /** * @var * * @ORM\ManyToOne(targetEntity="App\Entity\TypeGroupe") * @Groups({"Evaluation"}) */ private $typegroupe; /** * @var float * * @ORM\Column(type="decimal") * @Groups({"Evaluation"}) */ private $coefficient; /** * @return float */ public function getCoefficient(): float { return $this->coefficient; } /** * @return mixed */ public function getTypegroupe() { return $this->typegroupe; } /** * @param mixed $typegroupe */ public function setTypegroupe($typegroupe): void { $this->typegroupe = $typegroupe; } /** * @return \DateTime */ public function getDateEvaluation(): \DateTime { return $this->dateEvaluation; } /** * @param \DateTime $dateEvaluation */ public function setDateEvaluation(\DateTime $dateEvaluation): void { $this->dateEvaluation = $dateEvaluation; } /** * @return string */ public function getCommentaire(): string { return $this->commentaire; } /** * @param string $commentaire */ public function setCommentaire(string $commentaire): void { $this->commentaire = $commentaire; } /** * @param float $coefficient */ public function setCoefficient(float $coefficient): void { $this->coefficient = $coefficient; } /** * @return mixed */ public function getId() { return $this->id; } /** * @return \DateTime */ public function getCreated(): \DateTime { return $this->created; } /** * @param \DateTime $created */ public function setCreated(\DateTime $created): void { $this->created = $created; } /** * @return \DateTime */ public function getUpdated(): \DateTime { return $this->updated; } /** * @param \DateTime $updated */ public function setUpdated(\DateTime $updated): void { $this->updated = $updated; } /** * @return mixed */ public function getAuteur() { return $this->auteur; } /** * @param mixed $auteur */ public function setAuteur($auteur): void { $this->auteur = $auteur; } /** * @return mixed */ public function getAutorises() { return $this->autorises; } /** * @param mixed $autorises */ public function setAutorises($autorises): void { $this->autorises = $autorises; } /** * @return int */ public function getAnnee(): int { return $this->annee; } /** * @param int $annee */ public function setAnnee(int $annee): void { $this->annee = $annee; } /** * @return mixed */ public function getModule() { return $this->module; } /** * @param mixed $module */ public function setModule($module): void { $this->module = $module; } /** * @return bool */ public function isVisible(): bool { return $this->visible; } /** * @param bool $visible */ public function setVisible(bool $visible): void { $this->visible = $visible; } /** * @return bool */ public function isModifiable(): bool { return $this->modifiable; } /** * @param bool $modifiable */ public function setModifiable(bool $modifiable): void { $this->modifiable = $modifiable; } /** * @ORM\PreUpdate */ public function updateDate(): void { $this->setUpdated(new \Datetime()); } public function __construct() { $this->setCreated(new \Datetime()); $this->setUpdated(new \Datetime()); } }

I don't understand why "time" is missing. Maybe it is for created or updated fields ? Anyway, created and updated fields return with ApiPlatform an object with timezone ...

created: {timezone: {name: "Europe/Paris",…}, offset: 561, timestamp: -62169984561} offset:561 timestamp:-62169984561 timezone:{name: "Europe/Paris",…}

Thanks for your help. David

Answer1:

I've found a way to manage this error that works for me. I declare the DateTimeNormalizer in my config.yml like that :

datetime_normalizer: class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer public: false tags: [serializer.normalizer]

I hope that will help you !

Answer2:

In v1.1, using the class \DateTimeImmutable instead of \DateTime is what api-platform wait by default.

class Evaluation { public function __construct() { $this->setCreated(new \DateTimeImmutable()); $this->setUpdated(new \DateTimeImmutable()); } }

Recommend

  • Hibernate get Object by non ID , unique identifier
  • Eclipselink 2.4 + JPA + ManyToMany with additional column
  • Doctrine2 entity with varchar id don't insert id into database
  • Microsoft.Build.Evaluation.Project add folder and file force refresh of project in VS
  • How to return objects from multiple entity by joining by using spring data jpa?
  • Is a single constant value considered an expression?
  • Howto count a variable inside of innerHTML?
  • How to distinguish between attribute and element nodes returned from a Saxon XPathSelector
  • How to issue a command that produces infinite output and return immediately
  • Regex for incomplete lines within known start and end strings
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • iText RadioGroup/RadioButtons across multiple PdfPCells
  • Z3: Convert between FP and BitVector?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • How to access EntityManager inside Entity class in EJB3
  • Lost migrations and Azure database is now out of sync
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • JavaScriptCore crash on iOS9
  • Convert array of 8 bytes to signed long in C++
  • Importing jscolor library in angular 2
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app