MorphTo.php 8.7 KB

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