MorphOne.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. * @return Query
  77. */
  78. public function hasWhere($where = [])
  79. {
  80. throw new Exception('relation not support: hasWhere');
  81. }
  82. /**
  83. * 预载入关联查询
  84. * @access public
  85. * @param array $resultSet 数据集
  86. * @param string $relation 当前关联名
  87. * @param string $subRelation 子关联名
  88. * @param \Closure $closure 闭包
  89. * @return void
  90. */
  91. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  92. {
  93. $morphType = $this->morphType;
  94. $morphKey = $this->morphKey;
  95. $type = $this->type;
  96. $range = [];
  97. foreach ($resultSet as $result) {
  98. $pk = $result->getPk();
  99. // 获取关联外键列表
  100. if (isset($result->$pk)) {
  101. $range[] = $result->$pk;
  102. }
  103. }
  104. if (!empty($range)) {
  105. $data = $this->eagerlyMorphToOne([
  106. $morphKey => ['in', $range],
  107. $morphType => $type,
  108. ], $relation, $subRelation, $closure);
  109. // 关联属性名
  110. $attr = Loader::parseName($relation);
  111. // 关联数据封装
  112. foreach ($resultSet as $result) {
  113. if (!isset($data[$result->$pk])) {
  114. $relationModel = null;
  115. } else {
  116. $relationModel = $data[$result->$pk];
  117. $relationModel->setParent(clone $result);
  118. $relationModel->isUpdate(true);
  119. }
  120. $result->setRelation($attr, $relationModel);
  121. }
  122. }
  123. }
  124. /**
  125. * 预载入关联查询
  126. * @access public
  127. * @param Model $result 数据对象
  128. * @param string $relation 当前关联名
  129. * @param string $subRelation 子关联名
  130. * @param \Closure $closure 闭包
  131. * @return void
  132. */
  133. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  134. {
  135. $pk = $result->getPk();
  136. if (isset($result->$pk)) {
  137. $pk = $result->$pk;
  138. $data = $this->eagerlyMorphToOne([
  139. $this->morphKey => $pk,
  140. $this->morphType => $this->type,
  141. ], $relation, $subRelation, $closure);
  142. if (isset($data[$pk])) {
  143. $relationModel = $data[$pk];
  144. $relationModel->setParent(clone $result);
  145. $relationModel->isUpdate(true);
  146. } else {
  147. $relationModel = null;
  148. }
  149. $result->setRelation(Loader::parseName($relation), $relationModel);
  150. }
  151. }
  152. /**
  153. * 多态一对一 关联模型预查询
  154. * @access public
  155. * @param array $where 关联预查询条件
  156. * @param string $relation 关联名
  157. * @param string $subRelation 子关联
  158. * @param bool|\Closure $closure 闭包
  159. * @return array
  160. */
  161. protected function eagerlyMorphToOne($where, $relation, $subRelation = '', $closure = false)
  162. {
  163. // 预载入关联查询 支持嵌套预载入
  164. if ($closure) {
  165. call_user_func_array($closure, [ & $this]);
  166. }
  167. $list = $this->query->where($where)->with($subRelation)->find();
  168. $morphKey = $this->morphKey;
  169. // 组装模型数据
  170. $data = [];
  171. foreach ($list as $set) {
  172. $data[$set->$morphKey][] = $set;
  173. }
  174. return $data;
  175. }
  176. /**
  177. * 保存(新增)当前关联数据对象
  178. * @access public
  179. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  180. * @return Model|false
  181. */
  182. public function save($data)
  183. {
  184. if ($data instanceof Model) {
  185. $data = $data->getData();
  186. }
  187. // 保存关联表数据
  188. $pk = $this->parent->getPk();
  189. $model = new $this->model;
  190. $data[$this->morphKey] = $this->parent->$pk;
  191. $data[$this->morphType] = $this->type;
  192. return $model->save($data) ? $model : false;
  193. }
  194. /**
  195. * 执行基础查询(进执行一次)
  196. * @access protected
  197. * @return void
  198. */
  199. protected function baseQuery()
  200. {
  201. if (empty($this->baseQuery) && $this->parent->getData()) {
  202. $pk = $this->parent->getPk();
  203. $map[$this->morphKey] = $this->parent->$pk;
  204. $map[$this->morphType] = $this->type;
  205. $this->query->where($map);
  206. $this->baseQuery = true;
  207. }
  208. }
  209. }