Closed akserikawa closed 3 years ago
This is the PUT request I'm making right after creating one translation with en
locale:
PUT /products/e6a43c5f-3a94-4fb9-9cc7-fac72ada4bc6
{
"translations": {
"en": {
"id": 1,
"title": "New title",
"locale": "en"
}
}
}
And here are the Doctrine logs for that request:
[2021-01-13T22:34:42.239446+00:00] doctrine.DEBUG: SELECT t0.id AS id_1, t0.title AS title_2, t0.locale AS locale_3, t0.translatable_id AS translatable_id_4 FROM products_translations t0 WHERE t0.translatable_id = ? [{"Symfony\\Component\\Uid\\UuidV4":"e6a43c5f-3a94-4fb9-9cc7-fac72ada4bc6"}] []
[2021-01-13T22:34:42.243531+00:00] doctrine.DEBUG: SELECT NEXTVAL('products_translations_id_seq') [] []
[2021-01-13T22:34:42.245219+00:00] doctrine.DEBUG: "START TRANSACTION" [] []
[2021-01-13T22:34:42.245527+00:00] doctrine.DEBUG: INSERT INTO products_translations (id, title, locale, translatable_id) VALUES (?, ?, ?, ?) {"1":2,"2":"New title","3":"en","4":{"Symfony\\Component\\Uid\\UuidV4":"e6a43c5f-3a94-4fb9-9cc7-fac72ada4bc6"}} []
[2021-01-13T22:34:42.246025+00:00] doctrine.DEBUG: DELETE FROM products_translations WHERE id = ? [1] []
[2021-01-13T22:34:42.246313+00:00] doctrine.DEBUG: "COMMIT" [] []
As you can see, it's always trying to create new records and it's also deleting every existing translation for that product. Am I doing something wrong here? Has someone experienced the same problem? According to the docs, this should be a straight forward step but, somehow it's kinda messy. Any help on this would be much appreciated.
Btw, I'm running PHP 8.0.0
on API Platform v2.6-beta.1
and PostgreSQL 12.4
Hi @akserikawa, this is the default behaviour of the new version of ApiPlatform. PUT replaces the object with the new one. Try to using PATCH for editing translations. More info for operations: https://api-platform.com/docs/core/operations/
@akserikawa i was struggling with the same problem. i fixed it by providing the iri of the translation entity instead of the id.
{
"translations": {
"en": {
"@id": "/product_translations/1",
"title": "New title",
"locale": "en"
}
}
}
@paullla im pretty sure that PUT Requests are not doing a delete/insert. It will do an update.
From the docs: Note: Current PUT implementation behaves more or less like the PATCH method. Existing properties not included in the payload are not removed, their current values are preserved. To remove an existing property, its value must be explicitly set to null.
Hey @MarioZ2002 , thanks for the input. The flow might have changed in the newer versions, I will research it to see if everything still works ok with the translations.
I'm having a bit of trouble trying to edit translations. It seems every time I make a PUT request to my resource to update one translation it begins a new database transaction, inserts new items and then deletes every translation item in the table.
I've followed the guide and here are my entities:
Product.php
ProductTranslation.php