HasManyThrough.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. * @return Query
  74. */
  75. public function hasWhere($where = [])
  76. {
  77. throw new Exception('relation not support: hasWhere');
  78. }
  79. /**
  80. * 预载入关联查询
  81. * @access public
  82. * @param array $resultSet 数据集
  83. * @param string $relation 当前关联名
  84. * @param string $subRelation 子关联名
  85. * @param \Closure $closure 闭包
  86. * @return void
  87. */
  88. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  89. {}
  90. /**
  91. * 预载入关联查询 返回模型对象
  92. * @access public
  93. * @param Model $result 数据对象
  94. * @param string $relation 当前关联名
  95. * @param string $subRelation 子关联名
  96. * @param \Closure $closure 闭包
  97. * @return void
  98. */
  99. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  100. {}
  101. /**
  102. * 关联统计
  103. * @access public
  104. * @param Model $result 数据对象
  105. * @param \Closure $closure 闭包
  106. * @return integer
  107. */
  108. public function relationCount($result, $closure)
  109. {}
  110. /**
  111. * 执行基础查询(进执行一次)
  112. * @access protected
  113. * @return void
  114. */
  115. protected function baseQuery()
  116. {
  117. if (empty($this->baseQuery) && $this->parent->getData()) {
  118. $through = $this->through;
  119. $alias = Loader::parseName(basename(str_replace('\\', '/', $this->model)));
  120. $throughTable = $through::getTable();
  121. $pk = (new $through)->getPk();
  122. $throughKey = $this->throughKey;
  123. $modelTable = $this->parent->getTable();
  124. $this->query->field($alias . '.*')->alias($alias)
  125. ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
  126. ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
  127. ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
  128. $this->baseQuery = true;
  129. }
  130. }
  131. }