BelongsTo.php 6.8 KB

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