HasManyThrough.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\model\relation;
  12. use think\db\Query;
  13. use think\Exception;
  14. use think\Loader;
  15. use think\Model;
  16. use think\model\Relation;
  17. class HasManyThrough extends Relation
  18. {
  19. // 中间关联表外键
  20. protected $throughKey;
  21. // 中间表模型
  22. protected $through;
  23. /**
  24. * 构造函数
  25. * @access public
  26. * @param Model $parent 上级模型对象
  27. * @param string $model 模型名
  28. * @param string $through 中间模型名
  29. * @param string $foreignKey 关联外键
  30. * @param string $throughKey 关联外键
  31. * @param string $localKey 关联主键
  32. */
  33. public function __construct(Model $parent, $model, $through, $foreignKey, $throughKey, $localKey)
  34. {
  35. $this->parent = $parent;
  36. $this->model = $model;
  37. $this->through = $through;
  38. $this->foreignKey = $foreignKey;
  39. $this->throughKey = $throughKey;
  40. $this->localKey = $localKey;
  41. $this->query = (new $model)->db();
  42. }
  43. /**
  44. * 延迟获取关联数据
  45. * @param string $subRelation 子关联名
  46. * @param \Closure $closure 闭包查询条件
  47. * @return false|\PDOStatement|string|\think\Collection
  48. */
  49. public function getRelation($subRelation = '', $closure = null)
  50. {
  51. if ($closure) {
  52. call_user_func_array($closure, [ & $this->query]);
  53. }
  54. return $this->relation($subRelation)->select();
  55. }
  56. /**
  57. * 根据关联条件查询当前模型
  58. * @access public
  59. * @param string $operator 比较操作符
  60. * @param integer $count 个数
  61. * @param string $id 关联表的统计字段
  62. * @param string $joinType JOIN类型
  63. * @return Query
  64. */
  65. public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
  66. {
  67. return $this->parent;
  68. }
  69. /**
  70. * 根据关联条件查询当前模型
  71. * @access public
  72. * @param mixed $where 查询条件(数组或者闭包)
  73. * @param mixed $fields 字段
  74. * @return Query
  75. */
  76. public function hasWhere($where = [], $fields = null)
  77. {
  78. throw new Exception('relation not support: hasWhere');
  79. }
  80. /**
  81. * 预载入关联查询
  82. * @access public
  83. * @param array $resultSet 数据集
  84. * @param string $relation 当前关联名
  85. * @param string $subRelation 子关联名
  86. * @param \Closure $closure 闭包
  87. * @return void
  88. */
  89. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  90. {}
  91. /**
  92. * 预载入关联查询 返回模型对象
  93. * @access public
  94. * @param Model $result 数据对象
  95. * @param string $relation 当前关联名
  96. * @param string $subRelation 子关联名
  97. * @param \Closure $closure 闭包
  98. * @return void
  99. */
  100. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  101. {}
  102. /**
  103. * 关联统计
  104. * @access public
  105. * @param Model $result 数据对象
  106. * @param \Closure $closure 闭包
  107. * @return integer
  108. */
  109. public function relationCount($result, $closure)
  110. {}
  111. /**
  112. * 创建关联统计子查询
  113. * @access public
  114. * @param \Closure $closure 闭包
  115. * @param string $name 统计数据别名
  116. * @return string
  117. */
  118. public function getRelationCountQuery($closure, &$name = null)
  119. {
  120. throw new Exception('relation not support: withCount');
  121. }
  122. /**
  123. * 执行基础查询(进执行一次)
  124. * @access protected
  125. * @return void
  126. */
  127. protected function baseQuery()
  128. {
  129. if (empty($this->baseQuery) && $this->parent->getData()) {
  130. $through = $this->through;
  131. $alias = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
  132. $throughTable = $through::getTable();
  133. $pk = (new $through)->getPk();
  134. $throughKey = $this->throughKey;
  135. $modelTable = $this->parent->getTable();
  136. $this->query->field($alias . '.*')->alias($alias)
  137. ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
  138. ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
  139. ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
  140. $this->baseQuery = true;
  141. }
  142. }
  143. }