sayou
sayou

Reputation: 913

Symfony - Api Platform : Edit object if statement on POST request

I use API platform with Symfony5, and I created a service to verify a statement if it is correct.

This statement if is correct, I want to change an existing object instead of adding on a POST request.

So, on POST request I created an event with PRE_WRITE events priorities, and this event calls a service who verifies if the statement is correct if is true, I edit an existing object.

All that work correctly without any problem, but the POST request is always for adding a new object, so, I get a new line on the database table.

Is there any solution, to return 200 responses on the edit object?

Upvotes: 4

Views: 299

Answers (2)

ebyratu
ebyratu

Reputation: 293

Maybe you need to use PUT - for changes.

POST- to create a record

Upvotes: 0

gries
gries

Reputation: 1143

Try adding a Response to the kernel.view event like:

....
public function yourEvent(Event $event)
{
   // do your service stuff
   // ...
   $event->setResponse(new Response('ok', 200));

}

Upvotes: 1

Related Questions