What is the correct way to implement update of the entity? (Symfony 6)

Viewed 29

I have an event entity. What is the correct way to implement update of this entity? Our frontend-developer wants everything to be done with a single PUT request to the backend: changing the values of the title, description fields, as well as adding, deleting, and editing prices, event_dates, and event_dates. I made separate endpoints put /event/{id}, put /price/{id}, put event_date/{id} What can you recommend?

{
        "id": 504,
        "title": "First Event",
        "description": "Description of First Event",
        "created_at": "2022-08-16T08:42:11+00:00",
        "prices": [
            {
                "id": 4,
                "value": "12.99",
                "type": "regular",
                "is_entrance_free": false,
                "info": "some extra infos",
                "sorting": 7
            }
        ],
        "event_dates": [
            {
                "id": 2,
                "start_date": "2022-12-10",
                "end_date": "2022-12-31",
                "start_time": "13:00",
                "end_time": "16:00",
                "entrance_time": "12:30",
                "is_open_end": false,
                "info": "7"
            }
        ]
    }
1 Answers

One of the standard ways is to POST or PUT the JSON for either the complete new record, with everything changed, effectively overwriting the old one, but keeping the same ID, or a subset.

The request would go to an endpoint for PUT /event/{id} where the action reads the current record, and gets the JSON with the information to update.

    <?php

    // various use statements as required

    class ApiEventController extends AbstractController
    {
        #[Route('/api/event/{id}', methods: ['PUT'])]
        public function eventPut(Request $request, \App\Entity\Event $event)
        {
            // Security here - ensure the current user has permission to access & edit the event

            // a custom Deserializer can restrict what is used from the content
            // for example, ensuring the ID, or other fields are not changed.
            $serializer->deserialize(
                $request->getContent(),
                \App\Entity\Event::class,
                'json',
                [
                    // takes the new values, from the request content,
                    // and update the old value, fetched by ID from the URL
                    AbstractNormalizer::OBJECT_TO_POPULATE => $entity,
                ]
            );

            // $event is now the mix of the old, and new
            $entityManager->persist($event);
            $entityManager->flush();

            // return the updated event details
        }

Updating more complex contents (such as replacing an array of prices, or event_dates within the main entity) will need other deserializers and the configuration in the Event entity and others, so that the Symfony Serializer component understands what is required. https://symfony.com/doc/current/components/serializer.html and https://symfonycasts.com/tracks/symfony has more information and tutorials that well assist in learning more.

API-platform can make much of this simpler, for the simpler cases, but an understanding of the basics would be useful as a basis of understanding.

Related