BelongsTo.php 7.0 KB

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