MorphTo.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * @access public
  44. * @return Model
  45. */
  46. public function getModel()
  47. {
  48. $morphType = $this->morphType;
  49. $model = $this->parseModel($this->parent->$morphType);
  50. return (new $model);
  51. }
  52. /**
  53. * 延迟获取关联数据
  54. * @param string $subRelation 子关联名
  55. * @param \Closure $closure 闭包查询条件
  56. * @return mixed
  57. */
  58. public function getRelation($subRelation = '', $closure = null)
  59. {
  60. $morphKey = $this->morphKey;
  61. $morphType = $this->morphType;
  62. // 多态模型
  63. $model = $this->parseModel($this->parent->$morphType);
  64. // 主键数据
  65. $pk = $this->parent->$morphKey;
  66. $relationModel = (new $model)->relation($subRelation)->find($pk);
  67. if ($relationModel) {
  68. $relationModel->setParent(clone $this->parent);
  69. }
  70. return $relationModel;
  71. }
  72. /**
  73. * 根据关联条件查询当前模型
  74. * @access public
  75. * @param string $operator 比较操作符
  76. * @param integer $count 个数
  77. * @param string $id 关联表的统计字段
  78. * @param string $joinType JOIN类型
  79. * @return Query
  80. */
  81. public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
  82. {
  83. return $this->parent;
  84. }
  85. /**
  86. * 根据关联条件查询当前模型
  87. * @access public
  88. * @param mixed $where 查询条件(数组或者闭包)
  89. * @param mixed $fields 字段
  90. * @return Query
  91. */
  92. public function hasWhere($where = [], $fields = null)
  93. {
  94. throw new Exception('relation not support: hasWhere');
  95. }
  96. /**
  97. * 解析模型的完整命名空间
  98. * @access protected
  99. * @param string $model 模型名(或者完整类名)
  100. * @return string
  101. */
  102. protected function parseModel($model)
  103. {
  104. if (isset($this->alias[$model])) {
  105. $model = $this->alias[$model];
  106. }
  107. if (false === strpos($model, '\\')) {
  108. $path = explode('\\', get_class($this->parent));
  109. array_pop($path);
  110. array_push($path, Loader::parseName($model, 1));
  111. $model = implode('\\', $path);
  112. }
  113. return $model;
  114. }
  115. /**
  116. * 设置多态别名
  117. * @access public
  118. * @param array $alias 别名定义
  119. * @return $this
  120. */
  121. public function setAlias($alias)
  122. {
  123. $this->alias = $alias;
  124. return $this;
  125. }
  126. /**
  127. * 移除关联查询参数
  128. * @access public
  129. * @return $this
  130. */
  131. public function removeOption()
  132. {
  133. return $this;
  134. }
  135. /**
  136. * 预载入关联查询
  137. * @access public
  138. * @param array $resultSet 数据集
  139. * @param string $relation 当前关联名
  140. * @param string $subRelation 子关联名
  141. * @param \Closure $closure 闭包
  142. * @return void
  143. * @throws Exception
  144. */
  145. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  146. {
  147. $morphKey = $this->morphKey;
  148. $morphType = $this->morphType;
  149. $range = [];
  150. foreach ($resultSet as $result) {
  151. // 获取关联外键列表
  152. if (!empty($result->$morphKey)) {
  153. $range[$result->$morphType][] = $result->$morphKey;
  154. }
  155. }
  156. if (!empty($range)) {
  157. // 关联属性名
  158. $attr = Loader::parseName($relation);
  159. foreach ($range as $key => $val) {
  160. // 多态类型映射
  161. $model = $this->parseModel($key);
  162. $obj = new $model;
  163. $pk = $obj->getPk();
  164. $list = $obj->all($val, $subRelation);
  165. $data = [];
  166. foreach ($list as $k => $vo) {
  167. $data[$vo->$pk] = $vo;
  168. }
  169. foreach ($resultSet as $result) {
  170. if ($key == $result->$morphType) {
  171. // 关联模型
  172. if (!isset($data[$result->$morphKey])) {
  173. throw new Exception('relation data not exists :' . $this->model);
  174. } else {
  175. $relationModel = $data[$result->$morphKey];
  176. $relationModel->setParent(clone $result);
  177. $relationModel->isUpdate(true);
  178. $result->setRelation($attr, $relationModel);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * 预载入关联查询
  187. * @access public
  188. * @param Model $result 数据对象
  189. * @param string $relation 当前关联名
  190. * @param string $subRelation 子关联名
  191. * @param \Closure $closure 闭包
  192. * @return void
  193. */
  194. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  195. {
  196. $morphKey = $this->morphKey;
  197. $morphType = $this->morphType;
  198. // 多态类型映射
  199. $model = $this->parseModel($result->{$this->morphType});
  200. $this->eagerlyMorphToOne($model, $relation, $result, $subRelation);
  201. }
  202. /**
  203. * 关联统计
  204. * @access public
  205. * @param Model $result 数据对象
  206. * @param \Closure $closure 闭包
  207. * @return integer
  208. */
  209. public function relationCount($result, $closure)
  210. {
  211. }
  212. /**
  213. * 多态MorphTo 关联模型预查询
  214. * @access public
  215. * @param object $model 关联模型对象
  216. * @param string $relation 关联名
  217. * @param $result
  218. * @param string $subRelation 子关联
  219. * @return void
  220. */
  221. protected function eagerlyMorphToOne($model, $relation, &$result, $subRelation = '')
  222. {
  223. // 预载入关联查询 支持嵌套预载入
  224. $pk = $this->parent->{$this->morphKey};
  225. $data = (new $model)->with($subRelation)->find($pk);
  226. if ($data) {
  227. $data->setParent(clone $result);
  228. $data->isUpdate(true);
  229. }
  230. $result->setRelation(Loader::parseName($relation), $data ?: null);
  231. }
  232. /**
  233. * 添加关联数据
  234. * @access public
  235. * @param Model $model 关联模型对象
  236. * @param string $type 多态类型
  237. * @return Model
  238. */
  239. public function associate($model, $type = '')
  240. {
  241. $morphKey = $this->morphKey;
  242. $morphType = $this->morphType;
  243. $pk = $model->getPk();
  244. $this->parent->setAttr($morphKey, $model->$pk);
  245. $this->parent->setAttr($morphType, $type ?: get_class($model));
  246. $this->parent->save();
  247. return $this->parent->setRelation($this->relation, $model);
  248. }
  249. /**
  250. * 注销关联数据
  251. * @access public
  252. * @return Model
  253. */
  254. public function dissociate()
  255. {
  256. $morphKey = $this->morphKey;
  257. $morphType = $this->morphType;
  258. $this->parent->setAttr($morphKey, null);
  259. $this->parent->setAttr($morphType, null);
  260. $this->parent->save();
  261. return $this->parent->setRelation($this->relation, null);
  262. }
  263. /**
  264. * 创建关联统计子查询
  265. * @access public
  266. * @param \Closure $closure 闭包
  267. * @param string $name 统计数据别名
  268. * @return string
  269. */
  270. public function getRelationCountQuery($closure, &$name = null)
  271. {
  272. throw new Exception('relation not support: withCount');
  273. }
  274. }