Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!