Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:21:19+00:00 2026-05-28T16:21:19+00:00

We are using Symfony2 to create an API. When updating a record, we expect

  • 0

We are using Symfony2 to create an API. When updating a record, we expect the JSON input to represent a serialized updated entity. The JSON data will not contain some fields (for instance, CreatedAt should be set only once when the entity is created – and never updated). For instance, here is an example JSON PUT request:

{"id":"1","name":"anyname","description":"anydescription"}

Here is the PHP code on the Controller that should update the entity according to the JSON above (we are using JMS serializer Bundle):

$supplier = $serializer->deserialize(
    $this->get('request')->getContent(),
    'WhateverEntity',
    'json'
);

The EntityManger understands (correctly) that this is an update request (in fact, a SELECT query is implicitly triggered). The EntityManager also guess (not correctly) that CreatedAt property should be NULLified – it should instead keep the previous one.

How to fix this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:21:20+00:00Added an answer on May 28, 2026 at 4:21 pm

    I would use the Doctrine\ORM\Mapping\ClassMetadata API to discover existing fields in your entity.
    You can do following (I don’t know how JMSSerializerBundle works):

    //Unserialize data into $data
    $metadata = $em->getMetadataFactory()->getMetadataFor($FQCN);
    $id = array();
    foreach ($metadata->getIdentifierFieldNames() as $identifier) {
        if (!isset($data[$identifier])) {
            throw new InvalidArgumentException('Missing identifier');
        }
        $id[$identifier] = $data[$identifier];
        unset($data[$identifier]);
    }
    $entity = $em->find($metadata->getName(), $id);
    foreach ($metadata->getFieldNames() as $field) {
        //add necessary checks about field read/write operation feasibility here
        if (isset($data[$field])) {
            //careful! setters are not being called! Inflection is up to you if you need it!
            $metadata->setFieldValue($entity, $field, $data[$field]);
        }
    }
    $em->flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Symfony2 and Doctrine2 to create a blog engine. I will have three
I'm using Symfony2 and CraueFormFlowBundle to create a multi-step form. Everything is going well
Right now I'm using Doctrine2 in conjunction with Symfony2. I have an entity called
I'm using Symfony2 with sonataAdminBundle, sonataUserBundle and FOSUserBundle. I would like to create a
I'm trying to set up a json example using symfony2. I've created a test
I want to create a service in Symfony2 that will take in the current
I'm using Lightwindow v2 in a Symfony project to create a modal window (
I have a join-table which is created by using @ORM\ManyToMany annotation in Symfony2/Doctrine. It
I am trying to create a website using Symfony and PostgreSQL. I cant find
i want to create users for my webapplication. im using symfony. i wonder if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.