Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

684
Visualizações
Symfony Serialize Entity with Datetime / Deserialize fails

Im trying to save an Entity as a json file. The entity class also includes a datetime field:

class Ear
{
...
    /**
     * @ORM\Column(type="date")
     */
    private $date;
...
}

Then I create a new Ear:

$earLine = new Ear();
$earLine->setDate(new \DateTime());

Now I simply would think I can normalize and serialize this object:

$encoders = [new JsonEncoder()];
$normalizers = [new DateTimeNormalizer(), new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);

$json = $serializer->serialize($earLine, 'json');

which works fine. The json looks like this:

{
  "id": null,
  "date": "2021-05-31T00:00:00+02:00",
  ...
}

However upon trying to deserialize, it fails to denormalize the datetime.

$encoders = [new JsonEncoder()];
$normalizers = [new DateTimeNormalizer(), new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);

dd($serializer->deserialize($json, Ear::class, 'json'));

with the error Failed to denormalize attribute "date" value for class "App\Entity\Ear": Expected argument of type "DateTimeInterface", "string" given at property path "date".

I read the docs up and down and search stackoverflow. But it fails for not apparent reason, at least not for me.

Using Symfony 5.3.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda