2
0

PostTest.php 3.1 KB

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