实体定义

    • 一个实体类对应一张数据库的表结构
    • 实体对象代表了表的一行数据记录

    标记一个类是一个实体,无需多余参数

    • 定义实体对应实例,默认 default 实例 对,就是前面配置上的那个default实例:)
    • name 定义该实体映射的数据库表名

    参数:

    • name 定义类属性映射的表字段,没该注解标记的属性,不映射
    • type 定义字段数据更新时验证类型,暂时提供常见的数据类型延迟,后续会更多
    • 若定义type,可定义其它验证条件
    • 所有字段属性,必须要有gettersetter方法

    该注解标明当前类属性对应了数据库表中的主键,必须有这个注解标记

    1. /**
    2. * @Entity()
    3. * @Table(name="user")
    4. */
    5. class User extends Model
    6. {
    7. /**
    8. * 主键ID
    9. *
    10. * @Id()
    11. * @Column(name="id", type=Types::INT)
    12. * @var null|int
    13. */
    14. private $id;
    15. /**
    16. * 名称
    17. *
    18. * @Column(name="name", type=Types::STRING, length=20)
    19. * @Required()
    20. * @var null|string
    21. */
    22. private $name;
    23. /**
    24. * 年龄
    25. *
    26. * @Column(name="age", type=Types::INT)
    27. * @var int
    28. */
    29. private $age = 0;
    30. /**
    31. * 性别
    32. *
    33. * @Column(name="sex", type="int")
    34. * @var int
    35. */
    36. private $sex = 0;
    37. /**
    38. * 描述
    39. *
    40. * @Column(name="description", type="string")
    41. * @var string
    42. */
    43. /**
    44. * 非数据库字段,未定义映射关系
    45. *
    46. * @var mixed
    47. */
    48. private $otherProperty;
    49. /**
    50. * @return int|null
    51. */
    52. public function getId()
    53. {
    54. return $this->id;
    55. }
    56. /**
    57. * @param int|null $id
    58. */
    59. public function setId($id)
    60. {
    61. $this->id = $id;
    62. }
    63. /**
    64. * @return null|string
    65. */
    66. public function getName()
    67. {
    68. return $this->name;
    69. }
    70. /**
    71. * @param null|string $name
    72. */
    73. public function setName($name)
    74. {
    75. $this->name = $name;
    76. }
    77. /**
    78. * @return int
    79. */
    80. public function getAge(): int
    81. {
    82. return $this->age;
    83. }
    84. /**
    85. * @param int $age
    86. */
    87. {
    88. $this->age = $age;
    89. }
    90. /**
    91. * @return int
    92. */
    93. public function getSex(): int
    94. {
    95. return $this->sex;
    96. }
    97. /**
    98. * @param int $sex
    99. */
    100. public function setSex(int $sex)
    101. {
    102. $this->sex = $sex;
    103. }
    104. /**
    105. * @return string
    106. */
    107. public function getDesc(): string
    108. {
    109. return $this->desc;
    110. }
    111. /**
    112. * @param string $desc
    113. */
    114. public function setDesc(string $desc)
    115. {
    116. $this->desc = $desc;
    117. }
    118. /**
    119. * @return mixed
    120. */
    121. public function getOtherProperty()
    122. {
    123. return $this->otherProperty;
    124. }
    125. /**
    126. * @param mixed $otherProperty
    127. */
    128. public function setOtherProperty($otherProperty)
    129. {
    130. $this->otherProperty = $otherProperty;