MorphTo.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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)->db();
  166. $pk = $obj->getPk();
  167. // 预载入关联查询 支持嵌套预载入
  168. if ($closure instanceof \Closure) {
  169. $closure($obj);
  170. if ($field = $obj->getOptions('with_field')) {
  171. $obj->field($field)->removeOption('with_field');
  172. }
  173. }
  174. $list = $obj->all($val, $subRelation);
  175. $data = [];
  176. foreach ($list as $k => $vo) {
  177. $data[$vo->$pk] = $vo;
  178. }
  179. foreach ($resultSet as $result) {
  180. if ($key == $result->$morphType) {
  181. // 关联模型
  182. if (!isset($data[$result->$morphKey])) {
  183. $relationModel = null;
  184. } else {
  185. $relationModel = $data[$result->$morphKey];
  186. $relationModel->setParent(clone $result);
  187. $relationModel->isUpdate(true);
  188. }
  189. $result->setRelation($attr, $relationModel);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * 预载入关联查询
  197. * @access public
  198. * @param Model $result 数据对象
  199. * @param string $relation 当前关联名
  200. * @param string $subRelation 子关联名
  201. * @param \Closure $closure 闭包
  202. * @return void
  203. */
  204. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  205. {
  206. $morphKey = $this->morphKey;
  207. $morphType = $this->morphType;
  208. // 多态类型映射
  209. $model = $this->parseModel($result->{$this->morphType});
  210. $this->eagerlyMorphToOne($model, $relation, $result, $subRelation);
  211. }
  212. /**
  213. * 关联统计
  214. * @access public
  215. * @param Model $result 数据对象
  216. * @param \Closure $closure 闭包
  217. * @param string $aggregate 聚合查询方法
  218. * @param string $field 字段
  219. * @param string $name 统计字段别名
  220. * @return integer
  221. */
  222. public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '')
  223. {}
  224. /**
  225. * 多态MorphTo 关联模型预查询
  226. * @access protected
  227. * @param string $model 关联模型对象
  228. * @param string $relation 关联名
  229. * @param Model $result
  230. * @param string $subRelation 子关联
  231. * @return void
  232. */
  233. protected function eagerlyMorphToOne($model, $relation, &$result, $subRelation = '')
  234. {
  235. // 预载入关联查询 支持嵌套预载入
  236. $pk = $this->parent->{$this->morphKey};
  237. $data = (new $model)->with($subRelation)->find($pk);
  238. if ($data) {
  239. $data->setParent(clone $result);
  240. $data->isUpdate(true);
  241. }
  242. $result->setRelation(Loader::parseName($relation), $data ?: null);
  243. }
  244. /**
  245. * 添加关联数据
  246. * @access public
  247. * @param Model $model 关联模型对象
  248. * @param string $type 多态类型
  249. * @return Model
  250. */
  251. public function associate($model, $type = '')
  252. {
  253. $morphKey = $this->morphKey;
  254. $morphType = $this->morphType;
  255. $pk = $model->getPk();
  256. $this->parent->setAttr($morphKey, $model->$pk);
  257. $this->parent->setAttr($morphType, $type ?: get_class($model));
  258. $this->parent->save();
  259. return $this->parent->setRelation($this->relation, $model);
  260. }
  261. /**
  262. * 注销关联数据
  263. * @access public
  264. * @return Model
  265. */
  266. public function dissociate()
  267. {
  268. $morphKey = $this->morphKey;
  269. $morphType = $this->morphType;
  270. $this->parent->setAttr($morphKey, null);
  271. $this->parent->setAttr($morphType, null);
  272. $this->parent->save();
  273. return $this->parent->setRelation($this->relation, null);
  274. }
  275. }