多态一对多关联

比如我们有一张文章表,一张书籍表,一张评论表。文章和书籍的评论记录都在同一张评论表中。

  1. mysql> desc tb_article;
  2. +---------+------------------+------+-----+---------+----------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +---------+------------------+------+-----+---------+----------------+
  5. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  6. | title | varchar(32) | NO | | NULL | |
  7. | content | text | NO | | NULL | |
  8. +---------+------------------+------+-----+---------+----------------+
  9. mysql> desc tb_book;
  10. +-------+------------------+------+-----+---------+----------------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +-------+------------------+------+-----+---------+----------------+
  13. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  14. | title | varchar(32) | NO | | NULL | |
  15. +-------+------------------+------+-----+---------+----------------+
  16. mysql> desc tb_comment;
  17. +-------------+------------------+------+-----+---------+----------------+
  18. | Field | Type | Null | Key | Default | Extra |
  19. +-------------+------------------+------+-----+---------+----------------+
  20. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  21. | content | text | NO | | NULL | |
  22. | type | tinyint(4) | NO | | NULL | |
  23. | relation_id | int(10) unsigned | NO | | NULL | |
  24. +-------------+------------------+------+-----+---------+----------------+

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

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

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

定义

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

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

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

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

Article 模型

  1. /**
  2. * Article
  3. * @Entity
  4. * @Table(name="tb_article", id={"id"})
  5. * @property int $id
  6. * @property string $title
  7. * @property string $content
  8. * @property \Imi\Util\ArrayList $comments
  9. * @property \Imi\Util\ArrayList $taggables
  10. * @property \Imi\Util\ArrayList $tags
  11. */
  12. class Article extends Model
  13. {
  14. /**
  15. * id
  16. * @Column(name="id", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true)
  17. * @var int
  18. */
  19. protected $id;
  20. /**
  21. * 获取 id
  22. *
  23. * @return int
  24. */
  25. public function getId()
  26. {
  27. return $this->id;
  28. }
  29. /**
  30. * 赋值 id
  31. * @param int $id id
  32. * @return static
  33. */
  34. public function setId($id)
  35. {
  36. $this->id = $id;
  37. return $this;
  38. }
  39. /**
  40. * title
  41. * @Column(name="title", type="varchar", length=32, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  42. * @var string
  43. */
  44. protected $title;
  45. /**
  46. * 获取 title
  47. *
  48. * @return string
  49. */
  50. public function getTitle()
  51. {
  52. return $this->title;
  53. }
  54. /**
  55. * 赋值 title
  56. * @param string $title title
  57. * @return static
  58. */
  59. public function setTitle($title)
  60. {
  61. $this->title = $title;
  62. return $this;
  63. }
  64. /**
  65. * content
  66. * @Column(name="content", type="text", length=0, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  67. * @var string
  68. */
  69. protected $content;
  70. /**
  71. * 获取 content
  72. *
  73. * @return string
  74. */
  75. public function getContent()
  76. {
  77. return $this->content;
  78. }
  79. /**
  80. * 赋值 content
  81. * @param string $content content
  82. * @return static
  83. */
  84. public function setContent($content)
  85. {
  86. $this->content = $content;
  87. return $this;
  88. }
  89. /**
  90. * 评论
  91. *
  92. * @PolymorphicOneToMany(model=ImiDemo\HttpDemo\MainServer\Model\Comment::class, type="type", typeValue=1)
  93. * @JoinTo("relation_id")
  94. * @AutoSave(orphanRemoval=true)
  95. * @AutoDelete
  96. *
  97. * @var \Imi\Util\ArrayList
  98. */
  99. protected $comments;
  100. /**
  101. * Get 评论
  102. *
  103. * @return \ImiDemo\HttpDemo\MainServer\Model\Comment[]
  104. */
  105. public function getComments()
  106. {
  107. return $this->comments;
  108. }
  109. /**
  110. * Set 评论
  111. *
  112. * @param \ImiDemo\HttpDemo\MainServer\Model\Comment[] $comments 评论
  113. *
  114. * @return self
  115. */
  116. public function setComments($comments)
  117. {
  118. $this->comments = $comments;
  119. return $this;
  120. }
  121. }

主要参考$comments上的注解,与多态一对多一样。

Comment 模型

  1. /**
  2. * Comment
  3. * @Entity
  4. * @Table(name="tb_comment", id={"id"})
  5. * @property int $id
  6. * @property string $content
  7. * @property int $type
  8. * @property int $relationId
  9. */
  10. class Comment 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. * content
  39. * @Column(name="content", type="text", length=0, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  40. * @var string
  41. */
  42. protected $content;
  43. /**
  44. * 获取 content
  45. *
  46. * @return string
  47. */
  48. public function getContent()
  49. {
  50. return $this->content;
  51. }
  52. /**
  53. * 赋值 content
  54. * @param string $content content
  55. * @return static
  56. */
  57. public function setContent($content)
  58. {
  59. $this->content = $content;
  60. return $this;
  61. }
  62. /**
  63. * type
  64. * @Column(name="type", type="tinyint", length=4, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  65. * @var int
  66. */
  67. protected $type;
  68. /**
  69. * 获取 type
  70. *
  71. * @return int
  72. */
  73. public function getType()
  74. {
  75. return $this->type;
  76. }
  77. /**
  78. * 赋值 type
  79. * @param int $type type
  80. * @return static
  81. */
  82. public function setType($type)
  83. {
  84. $this->type = $type;
  85. return $this;
  86. }
  87. /**
  88. * relation_id
  89. * @Column(name="relation_id", type="int", length=10, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
  90. * @var int
  91. */
  92. protected $relationId;
  93. /**
  94. * 获取 relationId
  95. *
  96. * @return int
  97. */
  98. public function getRelationId()
  99. {
  100. return $this->relationId;
  101. }
  102. /**
  103. * 赋值 relationId
  104. * @param int $relationId relation_id
  105. * @return static
  106. */
  107. public function setRelationId($relationId)
  108. {
  109. $this->relationId = $relationId;
  110. return $this;
  111. }
  112. }

查询

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

评论模型反查文章模型

注解及用法与一对一相同