2
0

PostTest.php 3.7 KB

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