HasOne.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 HasOne 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. */
  26. public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER')
  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. }
  35. /**
  36. * 延迟获取关联数据
  37. * @param string $subRelation 子关联名
  38. * @param \Closure $closure 闭包查询条件
  39. * @return array|false|\PDOStatement|string|Model
  40. */
  41. public function getRelation($subRelation = '', $closure = null)
  42. {
  43. // 执行关联定义方法
  44. $localKey = $this->localKey;
  45. if ($closure) {
  46. call_user_func_array($closure, [ & $this->query]);
  47. }
  48. // 判断关联类型执行查询
  49. $relationModel = $this->query
  50. ->removeWhereField($this->foreignKey)
  51. ->where($this->foreignKey, $this->parent->$localKey)
  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. * @return Query
  63. */
  64. public function has()
  65. {
  66. $table = $this->query->getTable();
  67. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  68. $relation = basename(str_replace('\\', '/', $this->model));
  69. $localKey = $this->localKey;
  70. $foreignKey = $this->foreignKey;
  71. return $this->parent->db()
  72. ->alias($model)
  73. ->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) {
  74. $query->table([$table => $relation])->field($relation . '.' . $foreignKey)->whereExp($model . '.' . $localKey, '=' . $relation . '.' . $foreignKey);
  75. });
  76. }
  77. /**
  78. * 根据关联条件查询当前模型
  79. * @access public
  80. * @param mixed $where 查询条件(数组或者闭包)
  81. * @param mixed $fields 字段
  82. * @return Query
  83. */
  84. public function hasWhere($where = [], $fields = null)
  85. {
  86. $table = $this->query->getTable();
  87. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  88. $relation = basename(str_replace('\\', '/', $this->model));
  89. if (is_array($where)) {
  90. foreach ($where as $key => $val) {
  91. if (false === strpos($key, '.')) {
  92. $where[$relation . '.' . $key] = $val;
  93. unset($where[$key]);
  94. }
  95. }
  96. }
  97. $fields = $this->getRelationQueryFields($fields, $model);
  98. return $this->parent->db()->alias($model)
  99. ->field($fields)
  100. ->join([$table => $relation], $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
  101. ->where($where);
  102. }
  103. /**
  104. * 预载入关联查询(数据集)
  105. * @access public
  106. * @param array $resultSet 数据集
  107. * @param string $relation 当前关联名
  108. * @param string $subRelation 子关联名
  109. * @param \Closure $closure 闭包
  110. * @return void
  111. */
  112. protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
  113. {
  114. $localKey = $this->localKey;
  115. $foreignKey = $this->foreignKey;
  116. $range = [];
  117. foreach ($resultSet as $result) {
  118. // 获取关联外键列表
  119. if (isset($result->$localKey)) {
  120. $range[] = $result->$localKey;
  121. }
  122. }
  123. if (!empty($range)) {
  124. $this->query->removeWhereField($foreignKey);
  125. $data = $this->eagerlyWhere($this->query, [
  126. $foreignKey => [
  127. 'in',
  128. $range,
  129. ],
  130. ], $foreignKey, $relation, $subRelation, $closure);
  131. // 关联属性名
  132. $attr = Loader::parseName($relation);
  133. // 关联数据封装
  134. foreach ($resultSet as $result) {
  135. // 关联模型
  136. if (!isset($data[$result->$localKey])) {
  137. $relationModel = null;
  138. } else {
  139. $relationModel = $data[$result->$localKey];
  140. $relationModel->setParent(clone $result);
  141. $relationModel->isUpdate(true);
  142. }
  143. if (!empty($this->bindAttr)) {
  144. // 绑定关联属性
  145. $this->bindAttr($relationModel, $result, $this->bindAttr);
  146. } else {
  147. // 设置关联属性
  148. $result->setRelation($attr, $relationModel);
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * 预载入关联查询(数据)
  155. * @access public
  156. * @param Model $result 数据对象
  157. * @param string $relation 当前关联名
  158. * @param string $subRelation 子关联名
  159. * @param \Closure $closure 闭包
  160. * @return void
  161. */
  162. protected function eagerlyOne(&$result, $relation, $subRelation, $closure)
  163. {
  164. $localKey = $this->localKey;
  165. $foreignKey = $this->foreignKey;
  166. $this->query->removeWhereField($foreignKey);
  167. $data = $this->eagerlyWhere($this->query, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure);
  168. // 关联模型
  169. if (!isset($data[$result->$localKey])) {
  170. $relationModel = null;
  171. } else {
  172. $relationModel = $data[$result->$localKey];
  173. $relationModel->setParent(clone $result);
  174. $relationModel->isUpdate(true);
  175. }
  176. if (!empty($this->bindAttr)) {
  177. // 绑定关联属性
  178. $this->bindAttr($relationModel, $result, $this->bindAttr);
  179. } else {
  180. $result->setRelation(Loader::parseName($relation), $relationModel);
  181. }
  182. }
  183. /**
  184. * 执行基础查询(仅执行一次)
  185. * @access protected
  186. * @return void
  187. */
  188. protected function baseQuery()
  189. {
  190. if (empty($this->baseQuery)) {
  191. if (isset($this->parent->{$this->localKey})) {
  192. // 关联查询带入关联条件
  193. $this->query->where($this->foreignKey, '=', $this->parent->{$this->localKey});
  194. }
  195. $this->baseQuery = true;
  196. }
  197. }
  198. }