多态多对多关联

比如我们有一张文章表,一张书籍表,一张标签表,一张标签关联表。文章和书籍共用标签库。

  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_tags;
  17. +-------+------------------+------+-----+---------+----------------+
  18. | Field | Type | Null | Key | Default | Extra |
  19. +-------+------------------+------+-----+---------+----------------+
  20. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
  21. | name | varchar(32) | NO | | NULL | |
  22. +-------+------------------+------+-----+---------+----------------+
  23. mysql> desc tb_taggables;
  24. +---------------+------------------+------+-----+---------+-------+
  25. | Field | Type | Null | Key | Default | Extra |
  26. +---------------+------------------+------+-----+---------+-------+
  27. | tag_id | int(10) unsigned | NO | PRI | NULL | |
  28. | taggable_id | int(10) unsigned | NO | PRI | NULL | |
  29. | taggable_type | tinyint(4) | NO | PRI | NULL | |
  30. +---------------+------------------+------+-----+---------+-------+

定义

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

@PolymorphicManyToMany@PolymorphicToMany@JoinFromMiddle@JoinToMiddle@AutoSelect@AutoInsert@AutoUpdate@AutoSave@AutoDelete

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. /**
  16. * 标签关联
  17. *
  18. * @PolymorphicManyToMany(model="Tags", middle="Taggables", rightMany="tags", type="taggable_type", typeValue=1)
  19. * @JoinToMiddle(field="id", middleField="taggable_id")
  20. * @JoinFromMiddle(middleField="tag_id", field="id")
  21. *
  22. * @AutoInsert
  23. * @AutoUpdate(orphanRemoval=true)
  24. * @AutoSave
  25. * @AutoDelete
  26. *
  27. * @var \Imi\Util\ArrayList
  28. */
  29. protected $taggables;
  30. /**
  31. * Get 标签关联
  32. *
  33. * @return \Imi\Util\ArrayList
  34. */
  35. public function getTaggables()
  36. {
  37. return $this->taggables;
  38. }
  39. /**
  40. * Set 标签关联
  41. *
  42. * @param \Imi\Util\ArrayList $taggables 标签关联
  43. *
  44. * @return self
  45. */
  46. public function setTaggables(\Imi\Util\ArrayList $taggables)
  47. {
  48. $this->taggables = $taggables;
  49. return $this;
  50. }
  51. /**
  52. * 关联标签
  53. *
  54. * @var \Imi\Util\ArrayList
  55. */
  56. protected $tags;
  57. /**
  58. * Get 关联标签
  59. *
  60. * @return \Imi\Util\ArrayList
  61. */
  62. public function getTags()
  63. {
  64. return $this->tags;
  65. }
  66. /**
  67. * Set 关联标签
  68. *
  69. * @param \Imi\Util\ArrayList $tags 关联标签
  70. *
  71. * @return self
  72. */
  73. public function setTags($tags)
  74. {
  75. $this->tags = $tags;
  76. return $this;
  77. }
  78. }

Tags 模型

  1. /**
  2. * Tags
  3. * @Entity
  4. * @Table(name="tb_tags", id={"id"})
  5. * @property int $id
  6. * @property string $name
  7. */
  8. class Tags extends Model
  9. {
  10. // 省略其它……
  11. /**
  12. * 拥有本标签的文章列表
  13. *
  14. * @PolymorphicToMany(model=ImiDemo\HttpDemo\MainServer\Model\Article::class, modelField="id", type="taggable_type", typeValue=1, field="taggable_id", middle="Taggables")
  15. * @JoinToMiddle(field="id", middleField="taggable_id")
  16. * @JoinFromMiddle(middleField="tag_id", field="id")
  17. *
  18. * @AutoSelect(false)
  19. *
  20. * @var \Imi\Util\ArrayList
  21. */
  22. protected $articles;
  23. /**
  24. * Get 拥有本标签的模型列表
  25. *
  26. * @return \Imi\Util\ArrayList
  27. */
  28. public function getArticles()
  29. {
  30. return $this->articles;
  31. }
  32. /**
  33. * Set 拥有本标签的模型列表
  34. *
  35. * @param \Imi\Util\ArrayList $articles 拥有本标签的模型列表
  36. *
  37. * @return self
  38. */
  39. public function setArticles(\Imi\Util\ArrayList $articles)
  40. {
  41. $this->articles = $articles;
  42. return $this;
  43. }
  44. /**
  45. * 拥有本标签的书籍列表
  46. *
  47. * @PolymorphicToMany(model=ImiDemo\HttpDemo\MainServer\Model\Book::class, modelField="id", type="taggable_type", typeValue=2, field="taggable_id", middle="Taggables")
  48. * @JoinToMiddle(field="id", middleField="taggable_id")
  49. * @JoinFromMiddle(middleField="tag_id", field="id")
  50. *
  51. * @AutoSelect(false)
  52. *
  53. * @var \Imi\Util\ArrayList
  54. */
  55. protected $books;
  56. /**
  57. * Get 拥有本标签的书籍列表
  58. *
  59. * @return \Imi\Util\ArrayList
  60. */
  61. public function getBooks()
  62. {
  63. return $this->books;
  64. }
  65. /**
  66. * Set 拥有本标签的书籍列表
  67. *
  68. * @param \Imi\Util\ArrayList $books 拥有本标签的书籍列表
  69. *
  70. * @return self
  71. */
  72. public function setBooks(\Imi\Util\ArrayList $books)
  73. {
  74. $this->books = $books;
  75. return $this;
  76. }
  77. }

Taggables 模型

没有要设置的注解,不展示了

查询

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

标签反查文章和书籍

  1. $tag = Tags::find(1);
  2. $tag->queryRelations('articles', 'books');
  3. $articles = $tag->articles;
  4. $books = $tag->books;