Api platform PUT метод добавляет данные вместо замены

Есть сущность Offer

* @ApiResource(
 *     normalizationContext={"groups"={"offer:read"}, "swagger_definition_name"="Read"},
 *     denormalizationContext={"groups"={"offer:write"}, "swagger_definition_name"="Write"},
 *     itemOperations={
 *         "get",
 *         "put"={"security"="is_granted('ROLE_USER')"},
 *         "delete": {
 *             "security"="is_granted('ROLE_USER')"
 *         }
 *     },
 *     collectionOperations={
 *          "get",
 *          "post"={
 *              "security"="is_granted('ROLE_USER')"
 *          }
 *     },
 *     attributes={
 *          "order"={"id": "DESC"}
 *     },
 * )
class Offer
{
...

    /**
     * @var Collection страны оффера
     *
     * @ManyToMany(targetEntity=Country::class)
     * @JoinTable(name="offers_countries",
     *      joinColumns={@JoinColumn(name="offer_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="country_id", referencedColumnName="id")}
     * )
     * @Assert\NotBlank
     * @Assert\NotNull
     * @Assert\Count(min=1)
     * @Groups({"offer:read", "offer:write"})
     * @ApiSubresource(maxDepth=1000)
     */
    private Collection $countries;

    /**
     * @return Country[]|ArrayCollection<Country>
     */
    public function getCountries()
    {
        return $this->countries;
    }

    /**
     * @param Country $country
     * @return $this
     */
    public function addCountry(Country $country): self
    {
        if (!$this->countries->contains($country)) {
            $this->countries->add($country);
        }

        return $this;
    }
  ...
}

при PUT запросе типа https://example.com/api/offers/1 "countries": ["/api/countries/10"] к существующим странам добавляется страна с id 10. Подскажите что не так. Благодарю


Ответы (1 шт):

Автор решения: Garibaldi

Убрал type hint у свойства countries и изменил методы на

   public function getCountries()
    {
        return $this->countries;
    }


    public function setCountries($countries): Offer
    {
        $this->countries = $countries;

        return $this;
    }
→ Ссылка