Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 938
DAnnebicque Avatar asked Jan 27 '26 04:01

DAnnebicque


1 Answers

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 !

like image 172
Ugo T. Avatar answered Jan 28 '26 23:01

Ugo T.