多态一对一关联

比如我们有一个用户表,一个团队表,他们和头像表相关联。

  1. mysql> desc tb_user;
  2. +----------+------------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+------------------+------+-----+---------+----------------+
  5. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  6. | username | varchar(32) | NO | | NULL | |
  7. | age | tinyint(3) | NO | | NULL | |
  8. +----------+------------------+------+-----+---------+----------------+
  9. mysql> desc tb_team;
  10. +-------+------------------+------+-----+---------+----------------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +-------+------------------+------+-----+---------+----------------+
  13. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  14. | name | varchar(32) | NO | | NULL | |
  15. +-------+------------------+------+-----+---------+----------------+
  16. mysql> desc tb_avatar;
  17. +-------------+------------------+------+-----+---------+----------------+
  18. | Field | Type | Null | Key | Default | Extra |
  19. +-------------+------------------+------+-----+---------+----------------+
  20. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  21. | type | tinyint(4) | NO | MUL | NULL | |
  22. | relation_id | int(10) unsigned | NO | | NULL | |
  23. | url | varchar(255) | NO | | NULL | |
  24. +-------------+------------------+------+-----+---------+----------------+

具体示例代码可以看imi-demo项目,下面仅为简单展示。

具体示例代码可以看imi-demo项目,下面仅为简单展示。

具体示例代码可以看imi-demo项目,下面仅为简单展示。

定义

多态一对一关联会用到的注解:

@PolymorphicOneToOne@PolymorphicToOne@JoinFrom@JoinTo@AutoSelect@AutoInsert@AutoUpdate@AutoSave@AutoDelete

如 imi-demo 中代码所示,定义了一个$avatar属性,这个属性关联Avatar模型。

UseridAvatarrelation_id关联,并且type1的才关联。

User 模型

  1. /**
  2. * User
  3. * @Entity
  4. * @Table(name="tb_user", id={"id"})
  5. * @property int $id
  6. * @property string $username
  7. * @property \ImiDemo\HttpDemo\MainServer\Model\UserEx $ex
  8. * @property \Imi\Util\ArrayList $userRole
  9. * @property \Imi\Util\ArrayList $role
  10. * @property \ImiDemo\HttpDemo\MainServer\Model\Avatar $avatar
  11. */
  12. class User extends Model
  13. {
  14. /**
  15. * 头像
  16. *
  17. * @PolymorphicOneToOne(model=ImiDemo\HttpDemo\MainServer\Model\Avatar::class, type="type", typeValue=1)
  18. * @JoinTo("relation_id")
  19. * @AutoSave
  20. * @AutoDelete
  21. *
  22. * @var \ImiDemo\HttpDemo\MainServer\Model\Avatar
  23. */
  24. protected $avatar;
  25. /**
  26. * Get 头像
  27. *
  28. * @return \ImiDemo\HttpDemo\MainServer\Model\Avatar
  29. */
  30. public function getAvatar()
  31. {
  32. return $this->avatar;
  33. }
  34. /**
  35. * Set 头像
  36. *
  37. * @param \ImiDemo\HttpDemo\MainServer\Model\Avatar $avatar 头像
  38. *
  39. * @return self
  40. */
  41. public function setAvatar(\ImiDemo\HttpDemo\MainServer\Model\Avatar $avatar)
  42. {
  43. $this->avatar = $avatar;
  44. return $this;
  45. }
  46. }

@PolymorphicOneToOne注解中,type代表在Avatar模型中的字段名,typeValue代表匹配的值。

Avatar 模型

  1. /**
  2. * Avatar
  3. * @Entity
  4. * @Table(name="tb_avatar", id={"id"})
  5. * @property int $id
  6. * @property int $type
  7. * @property int $relationId
  8. * @property string $url
  9. */
  10. class Avatar extends Model
  11. {
  12. /**
  13. * id
  14. * @Column(name="id", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true)
  15. * @var int
  16. */
  17. protected $id;
  18. /**
  19. * 获取 id
  20. *
  21. * @return int
  22. */
  23. public function getId()
  24. {
  25. return $this->id;
  26. }
  27. /**
  28. * 赋值 id
  29. * @param int $id id
  30. * @return static
  31. */
  32. public function setId($id)
  33. {
  34. $this->id = $id;
  35. return $this;
  36. }
  37. /**
  38. * type
  39. * @Column(name="type", type="tinyint", length=4, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  40. * @var int
  41. */
  42. protected $type;
  43. /**
  44. * 获取 type
  45. *
  46. * @return int
  47. */
  48. public function getType()
  49. {
  50. return $this->type;
  51. }
  52. /**
  53. * 赋值 type
  54. * @param int $type type
  55. * @return static
  56. */
  57. public function setType($type)
  58. {
  59. $this->type = $type;
  60. return $this;
  61. }
  62. /**
  63. * relation_id
  64. * @Column(name="relation_id", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  65. * @var int
  66. */
  67. protected $relationId;
  68. /**
  69. * 获取 relationId
  70. *
  71. * @return int
  72. */
  73. public function getRelationId()
  74. {
  75. return $this->relationId;
  76. }
  77. /**
  78. * 赋值 relationId
  79. * @param int $relationId relation_id
  80. * @return static
  81. */
  82. public function setRelationId($relationId)
  83. {
  84. $this->relationId = $relationId;
  85. return $this;
  86. }
  87. /**
  88. * url
  89. * @Column(name="url", type="varchar", length=255, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  90. * @var string
  91. */
  92. protected $url;
  93. /**
  94. * 获取 url
  95. *
  96. * @return string
  97. */
  98. public function getUrl()
  99. {
  100. return $this->url;
  101. }
  102. /**
  103. * 赋值 url
  104. * @param string $url url
  105. * @return static
  106. */
  107. public function setUrl($url)
  108. {
  109. $this->url = $url;
  110. return $this;
  111. }
  112. /**
  113. * 对应用户
  114. *
  115. * @PolymorphicToOne(model=ImiDemo\HttpDemo\MainServer\Model\User::class, modelField="id", type="type", typeValue=1, field="relation_id")
  116. * @AutoSelect(false)
  117. *
  118. * @var \ImiDemo\HttpDemo\MainServer\Model\User
  119. */
  120. protected $user;
  121. /**
  122. * Get 对应用户
  123. *
  124. * @return \ImiDemo\HttpDemo\MainServer\Model\User
  125. */
  126. public function getUser()
  127. {
  128. return $this->user;
  129. }
  130. /**
  131. * Set 对应用户
  132. *
  133. * @param \ImiDemo\HttpDemo\MainServer\Model\User $user 对应用户
  134. *
  135. * @return self
  136. */
  137. public function setUser(\ImiDemo\HttpDemo\MainServer\Model\User $user)
  138. {
  139. $this->user = $user;
  140. return $this;
  141. }
  142. /**
  143. * 对应用户
  144. *
  145. * @PolymorphicToOne(model=ImiDemo\HttpDemo\MainServer\Model\Team::class, modelField="id", type="type", typeValue=2, field="relation_id")
  146. * @AutoSelect(false)
  147. *
  148. * @var \ImiDemo\HttpDemo\MainServer\Model\Team
  149. */
  150. protected $team;
  151. /**
  152. * Get 对应用户
  153. *
  154. * @return \ImiDemo\HttpDemo\MainServer\Model\Team
  155. */
  156. public function getTeam()
  157. {
  158. return $this->team;
  159. }
  160. /**
  161. * Set 对应用户
  162. *
  163. * @param \ImiDemo\HttpDemo\MainServer\Model\Team $team 对应用户
  164. *
  165. * @return self
  166. */
  167. public function setTeam(\ImiDemo\HttpDemo\MainServer\Model\Team $team)
  168. {
  169. $this->team = $team;
  170. return $this;
  171. }
  172. /**
  173. * 对应用户
  174. *
  175. * @PolymorphicToOne(model=ImiDemo\HttpDemo\MainServer\Model\User::class, modelField="id", type="type", typeValue=1, field="relation_id")
  176. * @PolymorphicToOne(model=ImiDemo\HttpDemo\MainServer\Model\Team::class, modelField="id", type="type", typeValue=2, field="relation_id")
  177. * @AutoSelect(false)
  178. *
  179. * @var \ImiDemo\HttpDemo\MainServer\Model\User|\ImiDemo\HttpDemo\MainServer\Model\Team
  180. */
  181. protected $relationModel;
  182. /**
  183. * Get 对应用户
  184. *
  185. * @return \ImiDemo\HttpDemo\MainServer\Model\User|\ImiDemo\HttpDemo\MainServer\Model\Team
  186. */
  187. public function getRelationModel()
  188. {
  189. return $this->relationModel;
  190. }
  191. /**
  192. * Set 对应用户
  193. *
  194. * @param \ImiDemo\HttpDemo\MainServer\Model\User|\ImiDemo\HttpDemo\MainServer\Model\Team $relationModel 对应用户
  195. *
  196. * @return self
  197. */
  198. public function setRelationModel($relationModel)
  199. {
  200. $this->relationModel = $relationModel;
  201. return $this;
  202. }
  203. }

Avatar 模型中,$user$team是指定类型的关联,只能取到对应模型的数据。

$relationModel会根据当前模型的type值,查询出对应模型的数据,类型不固定。

查询

常见的增删改查就不写了,和一对一关联一样用法

头像模型反查用户模型

智能类型查询

  1. $avatar = Avatar::find(1);
  2. $avatar->queryRelations('relationModel');
  3. $user = $avatar->relationModel;

指定类型查询

  1. $avatar = Avatar::find(1);
  2. $avatar->queryRelations('user');
  3. $user = $avatar->user;

同时查询多个

  1. $avatar = Avatar::find(1);
  2. $avatar->queryRelations('user', 'relationModel');
  3. $user1 = $avatar->user;
  4. $user2 = $avatar->relationModel;