赛亿官网

MorphMany.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 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. $list = $this->relation($subRelation)->select();
  54. $parent = clone $this->parent;
  55. foreach ($list as &$model) {
  56. $model->setParent($parent);
  57. }
  58. return $list;
  59. }
  60. /**
  61. * 根据关联条件查询当前模型
  62. * @access public
  63. * @param string $operator 比较操作符
  64. * @param integer $count 个数
  65. * @param string $id 关联表的统计字段
  66. * @param string $joinType JOIN类型
  67. * @return Query
  68. */
  69. public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
  70. {
  71. throw new Exception('relation not support: has');
  72. }
  73. /**
  74. * 根据关联条件查询当前模型
  75. * @access public
  76. * @param mixed $where 查询条件(数组或者闭包)
  77. * @return Query
  78. */
  79. public function hasWhere($where = [])
  80. {
  81. throw new Exception('relation not support: hasWhere');
  82. }
  83. /**
  84. * 预载入关联查询
  85. * @access public
  86. * @param array $resultSet 数据集
  87. * @param string $relation 当前关联名
  88. * @param string $subRelation 子关联名
  89. * @param \Closure $closure 闭包
  90. * @return void
  91. */
  92. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  93. {
  94. $morphType = $this->morphType;
  95. $morphKey = $this->morphKey;
  96. $type = $this->type;
  97. $range = [];
  98. foreach ($resultSet as $result) {
  99. $pk = $result->getPk();
  100. // 获取关联外键列表
  101. if (isset($result->$pk)) {
  102. $range[] = $result->$pk;
  103. }
  104. }
  105. if (!empty($range)) {
  106. $data = $this->eagerlyMorphToMany([
  107. $morphKey => ['in', $range],
  108. $morphType => $type,
  109. ], $relation, $subRelation, $closure);
  110. // 关联属性名
  111. $attr = Loader::parseName($relation);
  112. // 关联数据封装
  113. foreach ($resultSet as $result) {
  114. if (!isset($data[$result->$pk])) {
  115. $data[$result->$pk] = [];
  116. }
  117. foreach ($data[$result->$pk] as &$relationModel) {
  118. $relationModel->setParent(clone $result);
  119. $relationModel->isUpdate(true);
  120. }
  121. $result->setRelation($attr, $this->resultSetBuild($data[$result->$pk]));
  122. }
  123. }
  124. }
  125. /**
  126. * 预载入关联查询
  127. * @access public
  128. * @param Model $result 数据对象
  129. * @param string $relation 当前关联名
  130. * @param string $subRelation 子关联名
  131. * @param \Closure $closure 闭包
  132. * @return void
  133. */
  134. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  135. {
  136. $pk = $result->getPk();
  137. if (isset($result->$pk)) {
  138. $data = $this->eagerlyMorphToMany([
  139. $this->morphKey => $result->$pk,
  140. $this->morphType => $this->type,
  141. ], $relation, $subRelation, $closure);
  142. if (!isset($data[$result->$pk])) {
  143. $data[$result->$pk] = [];
  144. }
  145. foreach ($data[$result->$pk] as &$relationModel) {
  146. $relationModel->setParent(clone $result);
  147. $relationModel->isUpdate(true);
  148. }
  149. $result->setRelation(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk]));
  150. }
  151. }
  152. /**
  153. * 关联统计
  154. * @access public
  155. * @param Model $result 数据对象
  156. * @param \Closure $closure 闭包
  157. * @return integer
  158. */
  159. public function relationCount($result, $closure)
  160. {
  161. $pk = $result->getPk();
  162. $count = 0;
  163. if (isset($result->$pk)) {
  164. if ($closure) {
  165. call_user_func_array($closure, [ & $this->query]);
  166. }
  167. $count = $this->query->where([$this->morphKey => $result->$pk, $this->morphType => $this->type])->count();
  168. }
  169. return $count;
  170. }
  171. /**
  172. * 获取关联统计子查询
  173. * @access public
  174. * @param \Closure $closure 闭包
  175. * @return string
  176. */
  177. public function getRelationCountQuery($closure)
  178. {
  179. if ($closure) {
  180. call_user_func_array($closure, [ & $this->query]);
  181. }
  182. return $this->query->where([
  183. $this->morphKey => [
  184. 'exp',
  185. '=' . $this->parent->getTable() . '.' . $this->parent->getPk(),
  186. ],
  187. $this->morphType => $this->type,
  188. ])->fetchSql()->count();
  189. }
  190. /**
  191. * 多态一对多 关联模型预查询
  192. * @access public
  193. * @param array $where 关联预查询条件
  194. * @param string $relation 关联名
  195. * @param string $subRelation 子关联
  196. * @param bool|\Closure $closure 闭包
  197. * @return array
  198. */
  199. protected function eagerlyMorphToMany($where, $relation, $subRelation = '', $closure = false)
  200. {
  201. // 预载入关联查询 支持嵌套预载入
  202. if ($closure) {
  203. call_user_func_array($closure, [ & $this]);
  204. }
  205. $list = $this->query->where($where)->with($subRelation)->select();
  206. $morphKey = $this->morphKey;
  207. // 组装模型数据
  208. $data = [];
  209. foreach ($list as $set) {
  210. $data[$set->$morphKey][] = $set;
  211. }
  212. return $data;
  213. }
  214. /**
  215. * 保存(新增)当前关联数据对象
  216. * @access public
  217. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  218. * @return Model|false
  219. */
  220. public function save($data)
  221. {
  222. if ($data instanceof Model) {
  223. $data = $data->getData();
  224. }
  225. // 保存关联表数据
  226. $pk = $this->parent->getPk();
  227. $model = new $this->model;
  228. $data[$this->morphKey] = $this->parent->$pk;
  229. $data[$this->morphType] = $this->type;
  230. return $model->save($data) ? $model : false;
  231. }
  232. /**
  233. * 批量保存当前关联数据对象
  234. * @access public
  235. * @param array $dataSet 数据集
  236. * @return integer
  237. */
  238. public function saveAll(array $dataSet)
  239. {
  240. $result = false;
  241. foreach ($dataSet as $key => $data) {
  242. $result = $this->save($data);
  243. }
  244. return $result;
  245. }
  246. /**
  247. * 执行基础查询(进执行一次)
  248. * @access protected
  249. * @return void
  250. */
  251. protected function baseQuery()
  252. {
  253. if (empty($this->baseQuery) && $this->parent->getData()) {
  254. $pk = $this->parent->getPk();
  255. $map[$this->morphKey] = $this->parent->$pk;
  256. $map[$this->morphType] = $this->type;
  257. $this->query->where($map);
  258. $this->baseQuery = true;
  259. }
  260. }
  261. }