MorphOne.php 7.3 KB

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