Locastic / ApiPlatformTranslationBundle

Translation bundle for ApiPlatform based on Sylius translation
MIT License
85 stars 28 forks source link

PUT requests not updating translations, they always INSERT and DELETE all of them #36

Closed akserikawa closed 3 years ago

akserikawa commented 3 years ago

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

/**
 * @ApiResource(
 *      collectionOperations={
 *          "get"
 *      },
 *      itemOperations={
 *          "get",
 *          "put"={
 *              "security"="is_granted('ROLE_ADMIN')",
 *              "normalizationContext"={"groups"={"translations"}}
 *          },
 *          "delete"={"security"="is_granted('ROLE_ADMIN')"}
 *      },
 *      normalizationContext={"groups"={"product.read"}},
 *      denormalizationContext={"groups"={"product.write"}}
 * )
 * @ORM\Entity(repositoryClass=ProductRepository::class)
 * @ORM\Table("products")
 * @ORM\HasLifecycleCallbacks
 */
class Product extends AbstractTranslatable
{
    /* more properties */

    /**
     * @ORM\OneToMany(
     *      targetEntity=ProductTranslation::class,
     *      mappedBy="translatable",
     *      fetch="EXTRA_LAZY",
     *      indexBy="locale",
     *      cascade={"persist"},
     *      orphanRemoval=true
     * )
     *
     * @Groups({"product.read", "product.write", "translations"})
     */
    protected $translations;

    private string $title;

    public function __construct()
    {
        $this->translations = new ArrayCollection();
    }

    protected function createTranslation(): TranslationInterface
    {
        return new ProductTranslation();
    }

    public function setTitle(string $title)
    {
        $this->getTranslation()->setTitle($title);
    }

    public function getTitle(): ?string
    {
        return $this->getTranslation()->getTitle();
    }   
}

ProductTranslation.php

/**
 * @ORM\Entity(repositoryClass=ProductTranslationRepository::class)
 * @ORM\Table("products_translations")
 */
class ProductTranslation extends AbstractTranslation
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"product.read", "product.write", "translations"})
     */
    private int $id;

    /**
     * @ORM\ManyToOne(targetEntity=Product::class, inversedBy="translations")
     */
    protected $translatable;

    /**
     * @ORM\Column(type="string")
     * @Groups({"product.read", "product.write", "translations"})
     */
    private $title;

    /**
     * @ORM\Column(type="string")
     * @Groups({"product.read", "product.write", "translations"})
     */
    protected $locale;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }
}
akserikawa commented 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.

akserikawa commented 3 years ago

Btw, I'm running PHP 8.0.0 on API Platform v2.6-beta.1 and PostgreSQL 12.4

paullla commented 3 years ago

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/

MarioZ2002 commented 1 year ago

@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.

paullla commented 1 year ago

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.