BelongsTo.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\Loader;
  14. use think\Model;
  15. class BelongsTo extends OneToOne
  16. {
  17. /**
  18. * 构造函数
  19. * @access public
  20. * @param Model $parent 上级模型对象
  21. * @param string $model 模型名
  22. * @param string $foreignKey 关联外键
  23. * @param string $localKey 关联主键
  24. * @param string $joinType JOIN类型
  25. * @param string $relation 关联名
  26. */
  27. public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER', $relation = null)
  28. {
  29. $this->parent = $parent;
  30. $this->model = $model;
  31. $this->foreignKey = $foreignKey;
  32. $this->localKey = $localKey;
  33. $this->joinType = $joinType;
  34. $this->query = (new $model)->db();
  35. $this->relation = $relation;
  36. }
  37. /**
  38. * 延迟获取关联数据
  39. * @param string $subRelation 子关联名
  40. * @param \Closure $closure 闭包查询条件
  41. * @access public
  42. * @return array|false|\PDOStatement|string|Model
  43. */
  44. public function getRelation($subRelation = '', $closure = null)
  45. {
  46. $foreignKey = $this->foreignKey;
  47. if ($closure) {
  48. call_user_func_array($closure, [ & $this->query]);
  49. }
  50. $relationModel = $this->query
  51. ->removeWhereField($this->localKey)
  52. ->where($this->localKey, $this->parent->$foreignKey)
  53. ->relation($subRelation)
  54. ->find();
  55. if ($relationModel) {
  56. $relationModel->setParent(clone $this->parent);
  57. }
  58. return $relationModel;
  59. }
  60. /**
  61. * 根据关联条件查询当前模型
  62. * @access public
  63. * @param string $operator 比较操作符
  64. * @param integer $count 个数
  65. * @param string $id 关联表的统计字段
  66. * @return Query
  67. */
  68. public function has($operator = '>=', $count = 1, $id = '*')
  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. $table = $this->query->getTable();
  82. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  83. $relation = basename(str_replace('\\', '/', $this->model));
  84. if (is_array($where)) {
  85. foreach ($where as $key => $val) {
  86. if (false === strpos($key, '.')) {
  87. $where[$relation . '.' . $key] = $val;
  88. unset($where[$key]);
  89. }
  90. }
  91. }
  92. $fields = $this->getRelationQueryFields($fields, $model);
  93. return $this->parent->db()->alias($model)
  94. ->field($fields)
  95. ->join([$table => $relation], $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType)
  96. ->where($where);
  97. }
  98. /**
  99. * 预载入关联查询(数据集)
  100. * @access public
  101. * @param array $resultSet 数据集
  102. * @param string $relation 当前关联名
  103. * @param string $subRelation 子关联名
  104. * @param \Closure $closure 闭包
  105. * @return void
  106. */
  107. protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
  108. {
  109. $localKey = $this->localKey;
  110. $foreignKey = $this->foreignKey;
  111. $range = [];
  112. foreach ($resultSet as $result) {
  113. // 获取关联外键列表
  114. if (isset($result->$foreignKey)) {
  115. $range[] = $result->$foreignKey;
  116. }
  117. }
  118. if (!empty($range)) {
  119. $this->query->removeWhereField($localKey);
  120. $data = $this->eagerlyWhere($this->query, [
  121. $localKey => [
  122. 'in',
  123. $range,
  124. ],
  125. ], $localKey, $relation, $subRelation, $closure);
  126. // 关联属性名
  127. $attr = Loader::parseName($relation);
  128. // 关联数据封装
  129. foreach ($resultSet as $result) {
  130. // 关联模型
  131. if (!isset($data[$result->$foreignKey])) {
  132. $relationModel = null;
  133. } else {
  134. $relationModel = $data[$result->$foreignKey];
  135. $relationModel->setParent(clone $result);
  136. $relationModel->isUpdate(true);
  137. }
  138. if (!empty($this->bindAttr)) {
  139. // 绑定关联属性
  140. $this->bindAttr($relationModel, $result, $this->bindAttr);
  141. } else {
  142. // 设置关联属性
  143. $result->setRelation($attr, $relationModel);
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * 预载入关联查询(数据)
  150. * @access public
  151. * @param Model $result 数据对象
  152. * @param string $relation 当前关联名
  153. * @param string $subRelation 子关联名
  154. * @param \Closure $closure 闭包
  155. * @return void
  156. */
  157. protected function eagerlyOne(&$result, $relation, $subRelation, $closure)
  158. {
  159. $localKey = $this->localKey;
  160. $foreignKey = $this->foreignKey;
  161. $this->query->removeWhereField($localKey);
  162. $data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
  163. // 关联模型
  164. if (!isset($data[$result->$foreignKey])) {
  165. $relationModel = null;
  166. } else {
  167. $relationModel = $data[$result->$foreignKey];
  168. $relationModel->setParent(clone $result);
  169. $relationModel->isUpdate(true);
  170. }
  171. if (!empty($this->bindAttr)) {
  172. // 绑定关联属性
  173. $this->bindAttr($relationModel, $result, $this->bindAttr);
  174. } else {
  175. // 设置关联属性
  176. $result->setRelation(Loader::parseName($relation), $relationModel);
  177. }
  178. }
  179. /**
  180. * 添加关联数据
  181. * @access public
  182. * @param Model $model 关联模型对象
  183. * @return Model
  184. */
  185. public function associate($model)
  186. {
  187. $foreignKey = $this->foreignKey;
  188. $pk = $model->getPk();
  189. $this->parent->setAttr($foreignKey, $model->$pk);
  190. $this->parent->save();
  191. return $this->parent->setRelation($this->relation, $model);
  192. }
  193. /**
  194. * 注销关联数据
  195. * @access public
  196. * @return Model
  197. */
  198. public function dissociate()
  199. {
  200. $foreignKey = $this->foreignKey;
  201. $this->parent->setAttr($foreignKey, null);
  202. $this->parent->save();
  203. return $this->parent->setRelation($this->relation, null);
  204. }
  205. /**
  206. * 执行基础查询(仅执行一次)
  207. * @access protected
  208. * @return void
  209. */
  210. protected function baseQuery()
  211. {
  212. if (empty($this->baseQuery)) {
  213. if (isset($this->parent->{$this->foreignKey})) {
  214. // 关联查询带入关联条件
  215. $this->query->where($this->localKey, '=', $this->parent->{$this->foreignKey});
  216. }
  217. $this->baseQuery = true;
  218. }
  219. }
  220. }