HasMany.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 HasMany extends Relation
  18. {
  19. /**
  20. * 架构函数
  21. * @access public
  22. * @param Model $parent 上级模型对象
  23. * @param string $model 模型名
  24. * @param string $foreignKey 关联外键
  25. * @param string $localKey 关联主键
  26. */
  27. public function __construct(Model $parent, $model, $foreignKey, $localKey)
  28. {
  29. $this->parent = $parent;
  30. $this->model = $model;
  31. $this->foreignKey = $foreignKey;
  32. $this->localKey = $localKey;
  33. $this->query = (new $model)->db();
  34. }
  35. /**
  36. * 延迟获取关联数据
  37. * @param string $subRelation 子关联名
  38. * @param \Closure $closure 闭包查询条件
  39. * @return false|\PDOStatement|string|\think\Collection
  40. */
  41. public function getRelation($subRelation = '', $closure = null)
  42. {
  43. if ($closure) {
  44. call_user_func_array($closure, [& $this->query]);
  45. }
  46. return $this->relation($subRelation)->select();
  47. }
  48. /**
  49. * 预载入关联查询
  50. * @access public
  51. * @param array $resultSet 数据集
  52. * @param string $relation 当前关联名
  53. * @param string $subRelation 子关联名
  54. * @param \Closure $closure 闭包
  55. * @return void
  56. */
  57. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  58. {
  59. $localKey = $this->localKey;
  60. $range = [];
  61. foreach ($resultSet as $result) {
  62. // 获取关联外键列表
  63. if (isset($result->$localKey)) {
  64. $range[] = $result->$localKey;
  65. }
  66. }
  67. if (!empty($range)) {
  68. $data = $this->eagerlyOneToMany($this, [
  69. $this->foreignKey => [
  70. 'in',
  71. $range,
  72. ],
  73. ], $relation, $subRelation, $closure);
  74. // 关联属性名
  75. $attr = Loader::parseName($relation);
  76. // 关联数据封装
  77. foreach ($resultSet as $result) {
  78. if (!isset($data[$result->$localKey])) {
  79. $data[$result->$localKey] = [];
  80. }
  81. $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey]));
  82. }
  83. }
  84. }
  85. /**
  86. * 预载入关联查询
  87. * @access public
  88. * @param Model $result 数据对象
  89. * @param string $relation 当前关联名
  90. * @param string $subRelation 子关联名
  91. * @param \Closure $closure 闭包
  92. * @return void
  93. */
  94. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  95. {
  96. $localKey = $this->localKey;
  97. if (isset($result->$localKey)) {
  98. $data = $this->eagerlyOneToMany($this, [$this->foreignKey => $result->$localKey], $relation, $subRelation, $closure);
  99. // 关联数据封装
  100. if (!isset($data[$result->$localKey])) {
  101. $data[$result->$localKey] = [];
  102. }
  103. $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey]));
  104. }
  105. }
  106. /**
  107. * 关联统计
  108. * @access public
  109. * @param Model $result 数据对象
  110. * @param \Closure $closure 闭包
  111. * @return integer
  112. */
  113. public function relationCount($result, $closure)
  114. {
  115. $localKey = $this->localKey;
  116. $count = 0;
  117. if (isset($result->$localKey)) {
  118. if ($closure) {
  119. call_user_func_array($closure, [& $this->query]);
  120. }
  121. $count = $this->query->where([$this->foreignKey => $result->$localKey])->count();
  122. }
  123. return $count;
  124. }
  125. /**
  126. * 创建关联统计子查询
  127. * @access public
  128. * @param \Closure $closure 闭包
  129. * @return string
  130. */
  131. public function getRelationCountQuery($closure)
  132. {
  133. if ($closure) {
  134. call_user_func_array($closure, [& $this->query]);
  135. }
  136. return $this->query->where([
  137. $this->foreignKey => [
  138. 'exp',
  139. '=' . $this->parent->getTable() . '.' . $this->parent->getPk()
  140. ]
  141. ])->fetchSql()->count();
  142. }
  143. /**
  144. * 一对多 关联模型预查询
  145. * @access public
  146. * @param object $model 关联模型对象
  147. * @param array $where 关联预查询条件
  148. * @param string $relation 关联名
  149. * @param string $subRelation 子关联
  150. * @param bool $closure
  151. * @return array
  152. */
  153. protected function eagerlyOneToMany($model, $where, $relation, $subRelation = '', $closure = false)
  154. {
  155. $foreignKey = $this->foreignKey;
  156. // 预载入关联查询 支持嵌套预载入
  157. if ($closure) {
  158. call_user_func_array($closure, [& $model]);
  159. }
  160. $list = $model->where($where)->with($subRelation)->select();
  161. // 组装模型数据
  162. $data = [];
  163. foreach ($list as $set) {
  164. $data[$set->$foreignKey][] = $set;
  165. }
  166. return $data;
  167. }
  168. /**
  169. * 保存(新增)当前关联数据对象
  170. * @access public
  171. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  172. * @return integer
  173. */
  174. public function save($data)
  175. {
  176. if ($data instanceof Model) {
  177. $data = $data->getData();
  178. }
  179. // 保存关联表数据
  180. $data[$this->foreignKey] = $this->parent->{$this->localKey};
  181. $model = new $this->model;
  182. return $model->save($data);
  183. }
  184. /**
  185. * 批量保存当前关联数据对象
  186. * @access public
  187. * @param array $dataSet 数据集
  188. * @return integer
  189. */
  190. public function saveAll(array $dataSet)
  191. {
  192. $result = false;
  193. foreach ($dataSet as $key => $data) {
  194. $result = $this->save($data);
  195. }
  196. return $result;
  197. }
  198. /**
  199. * 根据关联条件查询当前模型
  200. * @access public
  201. * @param string $operator 比较操作符
  202. * @param integer $count 个数
  203. * @param string $id 关联表的统计字段
  204. * @return Query
  205. */
  206. public function has($operator = '>=', $count = 1, $id = '*')
  207. {
  208. $table = $this->query->getTable();
  209. return $this->parent->db()->alias('a')
  210. ->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType)
  211. ->group('b.' . $this->foreignKey)
  212. ->having('count(' . $id . ')' . $operator . $count);
  213. }
  214. /**
  215. * 根据关联条件查询当前模型
  216. * @access public
  217. * @param mixed $where 查询条件(数组或者闭包)
  218. * @return Query
  219. */
  220. public function hasWhere($where = [])
  221. {
  222. $table = $this->query->getTable();
  223. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  224. $relation = basename(str_replace('\\', '/', $this->model));
  225. if (is_array($where)) {
  226. foreach ($where as $key => $val) {
  227. if (false === strpos($key, '.')) {
  228. $where[$relation . '.' . $key] = $val;
  229. unset($where[$key]);
  230. }
  231. }
  232. }
  233. return $this->parent->db()->alias($model)
  234. ->field($model . '.*')
  235. ->join($table . ' ' . $relation, $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey)
  236. ->where($where);
  237. }
  238. /**
  239. * 执行基础查询(进执行一次)
  240. * @access protected
  241. * @return void
  242. */
  243. protected function baseQuery()
  244. {
  245. if (empty($this->baseQuery)) {
  246. if (isset($this->parent->{$this->localKey})) {
  247. // 关联查询带入关联条件
  248. $this->query->where($this->foreignKey, $this->parent->{$this->localKey});
  249. }
  250. $this->baseQuery = true;
  251. }
  252. }
  253. }