MorphMany.php 7.2 KB

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