MorphTo.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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\Exception;
  13. use think\Loader;
  14. use think\Model;
  15. use think\model\Relation;
  16. class MorphTo extends Relation
  17. {
  18. // 多态字段
  19. protected $morphKey;
  20. protected $morphType;
  21. // 多态别名
  22. protected $alias;
  23. protected $relation;
  24. /**
  25. * 构造函数
  26. * @access public
  27. * @param Model $parent 上级模型对象
  28. * @param string $morphType 多态字段名
  29. * @param string $morphKey 外键名
  30. * @param array $alias 多态别名定义
  31. * @param string $relation 关联名
  32. */
  33. public function __construct(Model $parent, $morphType, $morphKey, $alias = [], $relation = null)
  34. {
  35. $this->parent = $parent;
  36. $this->morphType = $morphType;
  37. $this->morphKey = $morphKey;
  38. $this->alias = $alias;
  39. $this->relation = $relation;
  40. }
  41. /**
  42. * 延迟获取关联数据
  43. * @param string $subRelation 子关联名
  44. * @param \Closure $closure 闭包查询条件
  45. * @return mixed
  46. */
  47. public function getRelation($subRelation = '', $closure = null)
  48. {
  49. $morphKey = $this->morphKey;
  50. $morphType = $this->morphType;
  51. // 多态模型
  52. $model = $this->parseModel($this->parent->$morphType);
  53. // 主键数据
  54. $pk = $this->parent->$morphKey;
  55. $relationModel = (new $model)->relation($subRelation)->find($pk);
  56. if ($relationModel) {
  57. $relationModel->setParent(clone $this->parent);
  58. }
  59. return $relationModel;
  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. return $this->parent;
  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 string $model 模型名(或者完整类名)
  89. * @return string
  90. */
  91. protected function parseModel($model)
  92. {
  93. if (isset($this->alias[$model])) {
  94. $model = $this->alias[$model];
  95. }
  96. if (false === strpos($model, '\\')) {
  97. $path = explode('\\', get_class($this->parent));
  98. array_pop($path);
  99. array_push($path, Loader::parseName($model, 1));
  100. $model = implode('\\', $path);
  101. }
  102. return $model;
  103. }
  104. /**
  105. * 设置多态别名
  106. * @access public
  107. * @param array $alias 别名定义
  108. * @return $this
  109. */
  110. public function setAlias($alias)
  111. {
  112. $this->alias = $alias;
  113. return $this;
  114. }
  115. /**
  116. * 移除关联查询参数
  117. * @access public
  118. * @return $this
  119. */
  120. public function removeOption()
  121. {
  122. return $this;
  123. }
  124. /**
  125. * 预载入关联查询
  126. * @access public
  127. * @param array $resultSet 数据集
  128. * @param string $relation 当前关联名
  129. * @param string $subRelation 子关联名
  130. * @param \Closure $closure 闭包
  131. * @return void
  132. * @throws Exception
  133. */
  134. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  135. {
  136. $morphKey = $this->morphKey;
  137. $morphType = $this->morphType;
  138. $range = [];
  139. foreach ($resultSet as $result) {
  140. // 获取关联外键列表
  141. if (!empty($result->$morphKey)) {
  142. $range[$result->$morphType][] = $result->$morphKey;
  143. }
  144. }
  145. if (!empty($range)) {
  146. // 关联属性名
  147. $attr = Loader::parseName($relation);
  148. foreach ($range as $key => $val) {
  149. // 多态类型映射
  150. $model = $this->parseModel($key);
  151. $obj = new $model;
  152. $pk = $obj->getPk();
  153. $list = $obj->all($val, $subRelation);
  154. $data = [];
  155. foreach ($list as $k => $vo) {
  156. $data[$vo->$pk] = $vo;
  157. }
  158. foreach ($resultSet as $result) {
  159. if ($key == $result->$morphType) {
  160. // 关联模型
  161. if (!isset($data[$result->$morphKey])) {
  162. throw new Exception('relation data not exists :' . $this->model);
  163. } else {
  164. $relationModel = $data[$result->$morphKey];
  165. $relationModel->setParent(clone $result);
  166. $relationModel->isUpdate(true);
  167. $result->setRelation($attr, $relationModel);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. /**
  175. * 预载入关联查询
  176. * @access public
  177. * @param Model $result 数据对象
  178. * @param string $relation 当前关联名
  179. * @param string $subRelation 子关联名
  180. * @param \Closure $closure 闭包
  181. * @return void
  182. */
  183. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  184. {
  185. $morphKey = $this->morphKey;
  186. $morphType = $this->morphType;
  187. // 多态类型映射
  188. $model = $this->parseModel($result->{$this->morphType});
  189. $this->eagerlyMorphToOne($model, $relation, $result, $subRelation);
  190. }
  191. /**
  192. * 关联统计
  193. * @access public
  194. * @param Model $result 数据对象
  195. * @param \Closure $closure 闭包
  196. * @return integer
  197. */
  198. public function relationCount($result, $closure)
  199. {
  200. }
  201. /**
  202. * 多态MorphTo 关联模型预查询
  203. * @access public
  204. * @param object $model 关联模型对象
  205. * @param string $relation 关联名
  206. * @param $result
  207. * @param string $subRelation 子关联
  208. * @return void
  209. */
  210. protected function eagerlyMorphToOne($model, $relation, &$result, $subRelation = '')
  211. {
  212. // 预载入关联查询 支持嵌套预载入
  213. $pk = $this->parent->{$this->morphKey};
  214. $data = (new $model)->with($subRelation)->find($pk);
  215. if ($data) {
  216. $data->setParent(clone $result);
  217. $data->isUpdate(true);
  218. }
  219. $result->setRelation(Loader::parseName($relation), $data ?: null);
  220. }
  221. /**
  222. * 添加关联数据
  223. * @access public
  224. * @param Model $model 关联模型对象
  225. * @param string $type 多态类型
  226. * @return Model
  227. */
  228. public function associate($model, $type = '')
  229. {
  230. $morphKey = $this->morphKey;
  231. $morphType = $this->morphType;
  232. $pk = $model->getPk();
  233. $this->parent->setAttr($morphKey, $model->$pk);
  234. $this->parent->setAttr($morphType, $type ?: get_class($model));
  235. $this->parent->save();
  236. return $this->parent->setRelation($this->relation, $model);
  237. }
  238. /**
  239. * 注销关联数据
  240. * @access public
  241. * @return Model
  242. */
  243. public function dissociate()
  244. {
  245. $morphKey = $this->morphKey;
  246. $morphType = $this->morphType;
  247. $this->parent->setAttr($morphKey, null);
  248. $this->parent->setAttr($morphType, null);
  249. $this->parent->save();
  250. return $this->parent->setRelation($this->relation, null);
  251. }
  252. }