|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of rekalogika/mapper package. |
| 7 | + * |
| 8 | + * (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE file |
| 11 | + * that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Rekalogika\Mapper\Tests\Fixtures\Doctrine; |
| 15 | + |
| 16 | +use Doctrine\Common\Collections\ArrayCollection; |
| 17 | +use Doctrine\Common\Collections\Collection; |
| 18 | +use Doctrine\ORM\Mapping as ORM; |
| 19 | + |
| 20 | +#[ORM\Entity] |
| 21 | +class EntityWithSingleIdentifier |
| 22 | +{ |
| 23 | + #[ORM\Id] |
| 24 | + #[ORM\Column(name: 'my_identifier', type: 'string', length: 255)] |
| 25 | + private string $myIdentifier; |
| 26 | + |
| 27 | + #[ORM\Column] |
| 28 | + private string $name; |
| 29 | + |
| 30 | + #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
| 31 | + #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'my_identifier')] |
| 32 | + private ?self $parent = null; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Collection<array-key,self> |
| 36 | + */ |
| 37 | + #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] |
| 38 | + private Collection $children; |
| 39 | + |
| 40 | + public function __construct(string $myIdentifier, string $name) |
| 41 | + { |
| 42 | + $this->myIdentifier = $myIdentifier; |
| 43 | + $this->name = $name; |
| 44 | + $this->children = new ArrayCollection(); |
| 45 | + } |
| 46 | + |
| 47 | + public function getMyIdentifier(): string |
| 48 | + { |
| 49 | + return $this->myIdentifier; |
| 50 | + } |
| 51 | + |
| 52 | + public function getName(): string |
| 53 | + { |
| 54 | + return $this->name; |
| 55 | + } |
| 56 | + |
| 57 | + public function getParent(): ?self |
| 58 | + { |
| 59 | + return $this->parent; |
| 60 | + } |
| 61 | + |
| 62 | + public function setParent(?self $parent): void |
| 63 | + { |
| 64 | + $this->parent = $parent; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return Collection<array-key,self> |
| 69 | + */ |
| 70 | + public function getChildren(): Collection |
| 71 | + { |
| 72 | + return $this->children; |
| 73 | + } |
| 74 | + |
| 75 | + public function addChild(self $child): void |
| 76 | + { |
| 77 | + if (!$this->children->contains($child)) { |
| 78 | + $this->children->add($child); |
| 79 | + $child->setParent($this); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public function removeChild(self $child): void |
| 84 | + { |
| 85 | + if ($this->children->removeElement($child)) { |
| 86 | + $child->setParent(null); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments