MorphMany.php 9.3 KB

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