HasManyThrough.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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;
  13. use think\db\Query;
  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 array $resultSet 数据集
  60. * @param string $relation 当前关联名
  61. * @param string $subRelation 子关联名
  62. * @param \Closure $closure 闭包
  63. * @param string $class 数据集对象名 为空表示数组
  64. * @return void
  65. */
  66. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
  67. {
  68. }
  69. /**
  70. * 预载入关联查询 返回模型对象
  71. * @access public
  72. * @param Model $result 数据对象
  73. * @param string $relation 当前关联名
  74. * @param string $subRelation 子关联名
  75. * @param \Closure $closure 闭包
  76. * @param string $class 数据集对象名 为空表示数组
  77. * @return void
  78. */
  79. public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
  80. {
  81. }
  82. /**
  83. * 关联统计
  84. * @access public
  85. * @param Model $result 数据对象
  86. * @param \Closure $closure 闭包
  87. * @return integer
  88. */
  89. public function relationCount($result, $closure)
  90. {
  91. }
  92. /**
  93. * 执行基础查询(进执行一次)
  94. * @access protected
  95. * @return void
  96. */
  97. protected function baseQuery()
  98. {
  99. if (empty($this->baseQuery)) {
  100. $through = $this->through;
  101. $model = $this->model;
  102. $alias = Loader::parseName(basename(str_replace('\\', '/', $model)));
  103. $throughTable = $through::getTable();
  104. $pk = (new $this->model)->getPk();
  105. $throughKey = $this->throughKey;
  106. $modelTable = $this->parent->getTable();
  107. $this->query->field($alias . '.*')->alias($alias)
  108. ->join($throughTable, $throughTable . '.' . $pk . '=' . $alias . '.' . $throughKey)
  109. ->join($modelTable, $modelTable . '.' . $this->localKey . '=' . $throughTable . '.' . $this->foreignKey)
  110. ->where($throughTable . '.' . $this->foreignKey, $this->parent->{$this->localKey});
  111. $this->baseQuery = true;
  112. }
  113. }
  114. }