BelongsTo.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. ->where($this->localKey, $this->parent->$foreignKey)
  52. ->relation($subRelation)
  53. ->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. * @return Query
  66. */
  67. public function has($operator = '>=', $count = 1, $id = '*')
  68. {
  69. return $this->parent;
  70. }
  71. /**
  72. * 根据关联条件查询当前模型
  73. * @access public
  74. * @param mixed $where 查询条件(数组或者闭包)
  75. * @param mixed $fields 字段
  76. * @return Query
  77. */
  78. public function hasWhere($where = [], $fields = null)
  79. {
  80. $table = $this->query->getTable();
  81. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  82. $relation = basename(str_replace('\\', '/', $this->model));
  83. if (is_array($where)) {
  84. foreach ($where as $key => $val) {
  85. if (false === strpos($key, '.')) {
  86. $where[$relation . '.' . $key] = $val;
  87. unset($where[$key]);
  88. }
  89. }
  90. }
  91. $fields = $this->getRelationQueryFields($fields, $model);
  92. return $this->parent->db()->alias($model)
  93. ->field($fields)
  94. ->join([$table => $relation], $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType)
  95. ->where($where);
  96. }
  97. /**
  98. * 预载入关联查询(数据集)
  99. * @access public
  100. * @param array $resultSet 数据集
  101. * @param string $relation 当前关联名
  102. * @param string $subRelation 子关联名
  103. * @param \Closure $closure 闭包
  104. * @return void
  105. */
  106. protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
  107. {
  108. $localKey = $this->localKey;
  109. $foreignKey = $this->foreignKey;
  110. $range = [];
  111. foreach ($resultSet as $result) {
  112. // 获取关联外键列表
  113. if (isset($result->$foreignKey)) {
  114. $range[] = $result->$foreignKey;
  115. }
  116. }
  117. if (!empty($range)) {
  118. $data = $this->eagerlyWhere($this->query, [
  119. $localKey => [
  120. 'in',
  121. $range,
  122. ],
  123. ], $localKey, $relation, $subRelation, $closure);
  124. // 关联属性名
  125. $attr = Loader::parseName($relation);
  126. // 关联数据封装
  127. foreach ($resultSet as $result) {
  128. // 关联模型
  129. if (!isset($data[$result->$foreignKey])) {
  130. $relationModel = null;
  131. } else {
  132. $relationModel = $data[$result->$foreignKey];
  133. $relationModel->setParent(clone $result);
  134. $relationModel->isUpdate(true);
  135. }
  136. if (!empty($this->bindAttr)) {
  137. // 绑定关联属性
  138. $this->bindAttr($relationModel, $result, $this->bindAttr);
  139. } else {
  140. // 设置关联属性
  141. $result->setRelation($attr, $relationModel);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * 预载入关联查询(数据)
  148. * @access public
  149. * @param Model $result 数据对象
  150. * @param string $relation 当前关联名
  151. * @param string $subRelation 子关联名
  152. * @param \Closure $closure 闭包
  153. * @return void
  154. */
  155. protected function eagerlyOne(&$result, $relation, $subRelation, $closure)
  156. {
  157. $localKey = $this->localKey;
  158. $foreignKey = $this->foreignKey;
  159. $data = $this->eagerlyWhere($this->query, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
  160. // 关联模型
  161. if (!isset($data[$result->$foreignKey])) {
  162. $relationModel = null;
  163. } else {
  164. $relationModel = $data[$result->$foreignKey];
  165. $relationModel->setParent(clone $result);
  166. $relationModel->isUpdate(true);
  167. }
  168. if (!empty($this->bindAttr)) {
  169. // 绑定关联属性
  170. $this->bindAttr($relationModel, $result, $this->bindAttr);
  171. } else {
  172. // 设置关联属性
  173. $result->setRelation(Loader::parseName($relation), $relationModel);
  174. }
  175. }
  176. /**
  177. * 添加关联数据
  178. * @access public
  179. * @param Model $model 关联模型对象
  180. * @return Model
  181. */
  182. public function associate($model)
  183. {
  184. $foreignKey = $this->foreignKey;
  185. $pk = $model->getPk();
  186. $this->parent->setAttr($foreignKey, $model->$pk);
  187. $this->parent->save();
  188. return $this->parent->setRelation($this->relation, $model);
  189. }
  190. /**
  191. * 注销关联数据
  192. * @access public
  193. * @return Model
  194. */
  195. public function dissociate()
  196. {
  197. $foreignKey = $this->foreignKey;
  198. $this->parent->setAttr($foreignKey, null);
  199. $this->parent->save();
  200. return $this->parent->setRelation($this->relation, null);
  201. }
  202. }