|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5998; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiResource; |
| 17 | +use ApiPlatform\Metadata\Post; |
| 18 | +use Doctrine\Common\Collections\ArrayCollection; |
| 19 | +use Doctrine\Common\Collections\Collection; |
| 20 | +use Doctrine\ORM\Mapping as ORM; |
| 21 | + |
| 22 | +#[ORM\Entity] |
| 23 | +#[ApiResource] |
| 24 | +#[Post( |
| 25 | + denormalizationContext: ['groups' => ['product:write']], |
| 26 | + input: SaveProduct::class, |
| 27 | +)] |
| 28 | +class Issue5998Product |
| 29 | +{ |
| 30 | + #[ORM\Id] |
| 31 | + #[ORM\GeneratedValue] |
| 32 | + #[ORM\Column] |
| 33 | + private ?int $id = null; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Collection<int, ProductCode> |
| 37 | + */ |
| 38 | + #[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductCode::class, cascade: ['persist'], orphanRemoval: true)] |
| 39 | + private Collection $codes; |
| 40 | + |
| 41 | + public function __construct() |
| 42 | + { |
| 43 | + $this->codes = new ArrayCollection(); |
| 44 | + } |
| 45 | + |
| 46 | + public function getId(): ?int |
| 47 | + { |
| 48 | + return $this->id; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @return Collection<int, ProductCode> |
| 53 | + */ |
| 54 | + public function getCodes(): Collection |
| 55 | + { |
| 56 | + return $this->codes; |
| 57 | + } |
| 58 | + |
| 59 | + public function addCode(ProductCode $code): void |
| 60 | + { |
| 61 | + if (!$this->codes->contains($code)) { |
| 62 | + $this->codes->add($code); |
| 63 | + $code->setProduct($this); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public function removeCode(ProductCode $code): void |
| 68 | + { |
| 69 | + if ($this->codes->removeElement($code)) { |
| 70 | + // set the owning side to null (unless already changed) |
| 71 | + if ($code->getProduct() === $this) { |
| 72 | + $code->setProduct(null); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments