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

239
Visualizações
Symfony Serializer cannot handle Doctrine Id on deserialize

I am trying to serialize and then deserialize a Doctrine Entity in Symfony 3. Well it does not work for the id property of course because there's no public accessor to the id by default. So what's the best option for that?

  1. Add a setId() method, but that's probably a bad idea and undermines the doctrine default behaviour.

  2. Use a custom Normalizer that uses reflection to construct the object by just accessing the field.

  3. Give the entity some custom toArray(), fromArray() functionality and use the array values instead of the object in the serialization.

Or am i missing something? Is there already something build into symfony for that usecase?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use object_to_populate option to do that :

    // retrieve entity to update from DB
    $oldProduct = $this->getDoctrine()->getRepository(Product::class)->find($request->request->get('id'));

    $product = $this->get('serializer')->deserialize($request->getContent(), Product::class, 'json', array('object_to_populate' => $oldProduct));

    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->persist($product);
    $entityManager->flush();

See Deserializing in an Existing Object documentation

about 4 years ago · Santiago Trujillo Relatório
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