2
0

PostTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM\Entity;
  3. use DateTime;
  4. use PhpDevCommunity\PaperORM\Collection\ObjectStorage;
  5. use PhpDevCommunity\PaperORM\Entity\EntityInterface;
  6. use PhpDevCommunity\PaperORM\Mapping\Column\SlugColumn;
  7. use PhpDevCommunity\PaperORM\Mapping\Column\StringColumn;
  8. use PhpDevCommunity\PaperORM\Mapping\Column\DateTimeColumn;
  9. use PhpDevCommunity\PaperORM\Mapping\Column\JoinColumn;
  10. use PhpDevCommunity\PaperORM\Mapping\Column\PrimaryKeyColumn;
  11. use PhpDevCommunity\PaperORM\Mapping\Entity;
  12. use PhpDevCommunity\PaperORM\Mapping\OneToMany;
  13. use Test\PhpDevCommunity\PaperORM\Repository\PostTestRepository;
  14. #[Entity(table : 'post', repository : PostTestRepository::class)]
  15. class PostTest implements EntityInterface
  16. {
  17. #[PrimaryKeyColumn]
  18. private ?int $id = null;
  19. #[StringColumn]
  20. private ?string $title = null;
  21. #[StringColumn]
  22. private ?string $content = null;
  23. #[SlugColumn(from : ['title'])]
  24. private ?string $slug = null;
  25. #[DateTimeColumn()]
  26. private ?DateTime $createdAt = null;
  27. #[JoinColumn(name: 'user_id', targetEntity: UserTest::class, nullable: true, unique: false, onDelete: JoinColumn::SET_NULL)]
  28. private ?UserTest $user = null;
  29. #[OneToMany(targetEntity: TagTest::class, mappedBy: 'post')]
  30. private ObjectStorage $tags;
  31. #[OneToMany(targetEntity: CommentTest::class, mappedBy: 'post')]
  32. private ObjectStorage $comments;
  33. public function __construct()
  34. {
  35. $this->tags = new ObjectStorage();
  36. $this->comments = new ObjectStorage();
  37. }
  38. static public function getTableName(): string
  39. {
  40. return 'post';
  41. }
  42. static public function getRepositoryName(): string
  43. {
  44. return PostTestRepository::class;
  45. }
  46. static public function columnsMapping(): array
  47. {
  48. if (PHP_VERSION_ID > 80000) {
  49. return [];
  50. }
  51. return [
  52. (new PrimaryKeyColumn())->bindProperty('id'),
  53. (new StringColumn())->bindProperty('title'),
  54. (new StringColumn())->bindProperty('content'),
  55. (new SlugColumn(null, ['title']))->bindProperty('slug'),
  56. (new DateTimeColumn( null))->bindProperty('createdAt'),
  57. (new JoinColumn('user_id', UserTest::class, 'id', true, false, JoinColumn::SET_NULL))->bindProperty('user'),
  58. (new OneToMany( TagTest::class, 'post'))->bindProperty('tags'),
  59. (new OneToMany( CommentTest::class, 'post'))->bindProperty('comments'),
  60. ];
  61. }
  62. public function getPrimaryKeyValue(): ?int
  63. {
  64. return $this->getId();
  65. }
  66. public function getId(): ?int
  67. {
  68. return $this->id;
  69. }
  70. public function setId(?int $id): PostTest
  71. {
  72. $this->id = $id;
  73. return $this;
  74. }
  75. public function getTitle(): ?string
  76. {
  77. return $this->title;
  78. }
  79. public function setTitle(?string $title): PostTest
  80. {
  81. $this->title = $title;
  82. return $this;
  83. }
  84. public function getContent(): ?string
  85. {
  86. return $this->content;
  87. }
  88. public function setContent(?string $content): PostTest
  89. {
  90. $this->content = $content;
  91. return $this;
  92. }
  93. public function getSlug(): ?string
  94. {
  95. return $this->slug;
  96. }
  97. public function setSlug(?string $slug): PostTest
  98. {
  99. $this->slug = $slug;
  100. return $this;
  101. }
  102. public function getCreatedAt(): ?DateTime
  103. {
  104. return $this->createdAt;
  105. }
  106. public function setCreatedAt(?DateTime $createdAt): PostTest
  107. {
  108. $this->createdAt = $createdAt;
  109. return $this;
  110. }
  111. public function getUser(): ?UserTest
  112. {
  113. return $this->user;
  114. }
  115. public function setUser(?UserTest $user): PostTest
  116. {
  117. $this->user = $user;
  118. return $this;
  119. }
  120. public function getTags(): ObjectStorage
  121. {
  122. return $this->tags;
  123. }
  124. public function addTag(TagTest $tag): PostTest
  125. {
  126. $this->tags->add($tag);
  127. return $this;
  128. }
  129. public function getComments(): ObjectStorage
  130. {
  131. return $this->comments;
  132. }
  133. public function addComment(CommentTest $comment): PostTest
  134. {
  135. $this->comments->add($comment);
  136. return $this;
  137. }
  138. }