MorphOne.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Query;
  13. use think\Exception;
  14. use think\Loader;
  15. use think\Model;
  16. use think\model\Relation;
  17. class MorphOne extends Relation
  18. {
  19. // 多态字段
  20. protected $morphKey;
  21. protected $morphType;
  22. // 多态类型
  23. protected $type;
  24. /**
  25. * 构造函数
  26. * @access public
  27. * @param Model $parent 上级模型对象
  28. * @param string $model 模型名
  29. * @param string $morphKey 关联外键
  30. * @param string $morphType 多态字段名
  31. * @param string $type 多态类型
  32. */
  33. public function __construct(Model $parent, $model, $morphKey, $morphType, $type)
  34. {
  35. $this->parent = $parent;
  36. $this->model = $model;
  37. $this->type = $type;
  38. $this->morphKey = $morphKey;
  39. $this->morphType = $morphType;
  40. $this->query = (new $model)->db();
  41. }
  42. /**
  43. * 延迟获取关联数据
  44. * @param string $subRelation 子关联名
  45. * @param \Closure $closure 闭包查询条件
  46. * @return false|\PDOStatement|string|\think\Collection
  47. */
  48. public function getRelation($subRelation = '', $closure = null)
  49. {
  50. if ($closure) {
  51. call_user_func_array($closure, [ & $this->query]);
  52. }
  53. $relationModel = $this->relation($subRelation)->find();
  54. if ($relationModel) {
  55. $relationModel->setParent(clone $this->parent);
  56. }
  57. return $relationModel;
  58. }
  59. /**
  60. * 根据关联条件查询当前模型
  61. * @access public
  62. * @param string $operator 比较操作符
  63. * @param integer $count 个数
  64. * @param string $id 关联表的统计字段
  65. * @param string $joinType JOIN类型
  66. * @return Query
  67. */
  68. public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
  69. {
  70. return $this->parent;
  71. }
  72. /**
  73. * 根据关联条件查询当前模型
  74. * @access public
  75. * @param mixed $where 查询条件(数组或者闭包)
  76. * @param mixed $fields 字段
  77. * @return Query
  78. */
  79. public function hasWhere($where = [], $fields = null)
  80. {
  81. throw new Exception('relation not support: hasWhere');
  82. }
  83. /**
  84. * 预载入关联查询
  85. * @access public
  86. * @param array $resultSet 数据集
  87. * @param string $relation 当前关联名
  88. * @param string $subRelation 子关联名
  89. * @param \Closure $closure 闭包
  90. * @return void
  91. */
  92. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  93. {
  94. $morphType = $this->morphType;
  95. $morphKey = $this->morphKey;
  96. $type = $this->type;
  97. $range = [];
  98. foreach ($resultSet as $result) {
  99. $pk = $result->getPk();
  100. // 获取关联外键列表
  101. if (isset($result->$pk)) {
  102. $range[] = $result->$pk;
  103. }
  104. }
  105. if (!empty($range)) {
  106. $data = $this->eagerlyMorphToOne([
  107. $morphKey => ['in', $range],
  108. $morphType => $type,
  109. ], $relation, $subRelation, $closure);
  110. // 关联属性名
  111. $attr = Loader::parseName($relation);
  112. // 关联数据封装
  113. foreach ($resultSet as $result) {
  114. if (!isset($data[$result->$pk])) {
  115. $relationModel = null;
  116. } else {
  117. $relationModel = $data[$result->$pk];
  118. $relationModel->setParent(clone $result);
  119. $relationModel->isUpdate(true);
  120. }
  121. $result->setRelation($attr, $relationModel);
  122. }
  123. }
  124. }
  125. /**
  126. * 预载入关联查询
  127. * @access public
  128. * @param Model $result 数据对象
  129. * @param string $relation 当前关联名
  130. * @param string $subRelation 子关联名
  131. * @param \Closure $closure 闭包
  132. * @return void
  133. */
  134. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  135. {
  136. $pk = $result->getPk();
  137. if (isset($result->$pk)) {
  138. $pk = $result->$pk;
  139. $data = $this->eagerlyMorphToOne([
  140. $this->morphKey => $pk,
  141. $this->morphType => $this->type,
  142. ], $relation, $subRelation, $closure);
  143. if (isset($data[$pk])) {
  144. $relationModel = $data[$pk];
  145. $relationModel->setParent(clone $result);
  146. $relationModel->isUpdate(true);
  147. } else {
  148. $relationModel = null;
  149. }
  150. $result->setRelation(Loader::parseName($relation), $relationModel);
  151. }
  152. }
  153. /**
  154. * 多态一对一 关联模型预查询
  155. * @access public
  156. * @param array $where 关联预查询条件
  157. * @param string $relation 关联名
  158. * @param string $subRelation 子关联
  159. * @param bool|\Closure $closure 闭包
  160. * @return array
  161. */
  162. protected function eagerlyMorphToOne($where, $relation, $subRelation = '', $closure = false)
  163. {
  164. // 预载入关联查询 支持嵌套预载入
  165. if ($closure) {
  166. call_user_func_array($closure, [ & $this]);
  167. }
  168. $list = $this->query->where($where)->with($subRelation)->find();
  169. $morphKey = $this->morphKey;
  170. // 组装模型数据
  171. $data = [];
  172. foreach ($list as $set) {
  173. $data[$set->$morphKey][] = $set;
  174. }
  175. return $data;
  176. }
  177. /**
  178. * 保存(新增)当前关联数据对象
  179. * @access public
  180. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  181. * @return Model|false
  182. */
  183. public function save($data)
  184. {
  185. if ($data instanceof Model) {
  186. $data = $data->getData();
  187. }
  188. // 保存关联表数据
  189. $pk = $this->parent->getPk();
  190. $data[$this->morphKey] = $this->parent->$pk;
  191. $data[$this->morphType] = $this->type;
  192. $model = new $this->model();
  193. return $model->save() ? $model : false;
  194. }
  195. /**
  196. * 创建关联对象实例
  197. * @param array $data
  198. * @return Model
  199. */
  200. public function make($data = [])
  201. {
  202. if ($data instanceof Model) {
  203. $data = $data->getData();
  204. }
  205. // 保存关联表数据
  206. $pk = $this->parent->getPk();
  207. $data[$this->morphKey] = $this->parent->$pk;
  208. $data[$this->morphType] = $this->type;
  209. return new $this->model($data);
  210. }
  211. /**
  212. * 执行基础查询(进执行一次)
  213. * @access protected
  214. * @return void
  215. */
  216. protected function baseQuery()
  217. {
  218. if (empty($this->baseQuery) && $this->parent->getData()) {
  219. $pk = $this->parent->getPk();
  220. $map[$this->morphKey] = $this->parent->$pk;
  221. $map[$this->morphType] = $this->type;
  222. $this->query->where($map);
  223. $this->baseQuery = true;
  224. }
  225. }
  226. /**
  227. * 创建关联统计子查询
  228. * @access public
  229. * @param \Closure $closure 闭包
  230. * @param string $name 统计数据别名
  231. * @return string
  232. */
  233. public function getRelationCountQuery($closure, &$name = null)
  234. {
  235. throw new Exception('relation not support: withCount');
  236. }
  237. }