UserTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Test\PhpDevCommunity\PaperORM\Entity;
  3. use PhpDevCommunity\PaperORM\Collection\ObjectStorage;
  4. use PhpDevCommunity\PaperORM\Entity\EntityInterface;
  5. use PhpDevCommunity\PaperORM\Mapping\Column\BoolColumn;
  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 : 'user', repository : null)]
  14. class UserTest implements EntityInterface
  15. {
  16. #[PrimaryKeyColumn]
  17. private ?int $id = null;
  18. #[StringColumn]
  19. private ?string $firstname = null;
  20. #[StringColumn]
  21. private ?string $lastname = null;
  22. #[StringColumn]
  23. private ?string $email = null;
  24. #[StringColumn]
  25. private ?string $password = null;
  26. #[BoolColumn(name: 'is_active')]
  27. private bool $active = false;
  28. #[DateTimeColumn(name: 'created_at')]
  29. private ?\DateTime $createdAt = null;
  30. #[OneToMany(targetEntity: PostTest::class, mappedBy: 'user')]
  31. private ObjectStorage $posts;
  32. #[JoinColumn(name: 'last_post_id', targetEntity: PostTest::class, nullable: true, unique: true, onDelete: JoinColumn::SET_NULL)]
  33. private ?PostTest $lastPost = null;
  34. public function __construct()
  35. {
  36. $this->posts = new ObjectStorage();
  37. $this->createdAt = new \DateTime();
  38. }
  39. static public function getTableName(): string
  40. {
  41. return 'user';
  42. }
  43. static public function getRepositoryName(): ?string
  44. {
  45. return null;
  46. }
  47. static public function columnsMapping(): array
  48. {
  49. if (PHP_VERSION_ID > 80000) {
  50. return [];
  51. }
  52. return [
  53. (new PrimaryKeyColumn())->bindProperty('id'),
  54. (new StringColumn())->bindProperty('firstname'),
  55. (new StringColumn())->bindProperty('lastname'),
  56. (new StringColumn())->bindProperty('email'),
  57. (new StringColumn())->bindProperty('password'),
  58. (new BoolColumn( 'is_active'))->bindProperty('active'),
  59. (new DateTimeColumn( 'created_at'))->bindProperty('createdAt'),
  60. (new OneToMany( PostTest::class, 'user'))->bindProperty('posts'),
  61. (new JoinColumn( 'last_post_id', PostTest::class, 'id', true, true, JoinColumn::SET_NULL))->bindProperty('lastPost'),
  62. ];
  63. }
  64. public function getPrimaryKeyValue() : ?int
  65. {
  66. return $this->getId();
  67. }
  68. public function getId(): ?int
  69. {
  70. return $this->id;
  71. }
  72. public function setId(?int $id): UserTest
  73. {
  74. $this->id = $id;
  75. return $this;
  76. }
  77. public function getFirstname(): ?string
  78. {
  79. return $this->firstname;
  80. }
  81. public function setFirstname(?string $firstname): UserTest
  82. {
  83. $this->firstname = $firstname;
  84. return $this;
  85. }
  86. public function getLastname(): ?string
  87. {
  88. return $this->lastname;
  89. }
  90. public function setLastname(?string $lastname): UserTest
  91. {
  92. $this->lastname = $lastname;
  93. return $this;
  94. }
  95. public function getEmail(): ?string
  96. {
  97. return $this->email;
  98. }
  99. public function setEmail(?string $email): UserTest
  100. {
  101. $this->email = $email;
  102. return $this;
  103. }
  104. public function getPassword(): ?string
  105. {
  106. return $this->password;
  107. }
  108. public function setPassword(?string $password): UserTest
  109. {
  110. $this->password = $password;
  111. return $this;
  112. }
  113. public function isActive(): bool
  114. {
  115. return $this->active;
  116. }
  117. public function setActive(bool $active): UserTest
  118. {
  119. $this->active = $active;
  120. return $this;
  121. }
  122. public function getCreatedAt(): ?\DateTime
  123. {
  124. return $this->createdAt;
  125. }
  126. public function setCreatedAt(?\DateTime $createdAt): UserTest
  127. {
  128. $this->createdAt = $createdAt;
  129. return $this;
  130. }
  131. public function getPosts(): ObjectStorage
  132. {
  133. return $this->posts;
  134. }
  135. public function addPost(PostTest $post): UserTest
  136. {
  137. $this->posts->add($post);
  138. return $this;
  139. }
  140. public function getLastPost(): ?PostTest
  141. {
  142. return $this->lastPost;
  143. }
  144. public function setLastPost(?PostTest $lastPost): UserTest
  145. {
  146. $this->lastPost = $lastPost;
  147. return $this;
  148. }
  149. }