HasOne.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();
  50. if ($relationModel) {
  51. $relationModel->setParent(clone $this->parent);
  52. }
  53. return $relationModel;
  54. }
  55. /**
  56. * 根据关联条件查询当前模型
  57. * @access public
  58. * @return Query
  59. */
  60. public function has()
  61. {
  62. $table = $this->query->getTable();
  63. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  64. $relation = basename(str_replace('\\', '/', $this->model));
  65. $localKey = $this->localKey;
  66. $foreignKey = $this->foreignKey;
  67. return $this->parent->db()
  68. ->alias($model)
  69. ->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) {
  70. $query->table([$table => $relation])->field($relation . '.' . $foreignKey)->whereExp($model . '.' . $localKey, '=' . $relation . '.' . $foreignKey);
  71. });
  72. }
  73. /**
  74. * 根据关联条件查询当前模型
  75. * @access public
  76. * @param mixed $where 查询条件(数组或者闭包)
  77. * @param mixed $fields 字段
  78. * @return Query
  79. */
  80. public function hasWhere($where = [], $fields = null)
  81. {
  82. $table = $this->query->getTable();
  83. $model = basename(str_replace('\\', '/', get_class($this->parent)));
  84. $relation = basename(str_replace('\\', '/', $this->model));
  85. if (is_array($where)) {
  86. foreach ($where as $key => $val) {
  87. if (false === strpos($key, '.')) {
  88. $where[$relation . '.' . $key] = $val;
  89. unset($where[$key]);
  90. }
  91. }
  92. }
  93. $fields = $this->getRelationQueryFields($fields, $model);
  94. return $this->parent->db()->alias($model)
  95. ->field($fields)
  96. ->join([$table => $relation], $model . '.' . $this->localKey . '=' . $relation . '.' . $this->foreignKey, $this->joinType)
  97. ->where($where);
  98. }
  99. /**
  100. * 预载入关联查询(数据集)
  101. * @access public
  102. * @param array $resultSet 数据集
  103. * @param string $relation 当前关联名
  104. * @param string $subRelation 子关联名
  105. * @param \Closure $closure 闭包
  106. * @return void
  107. */
  108. protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
  109. {
  110. $localKey = $this->localKey;
  111. $foreignKey = $this->foreignKey;
  112. $range = [];
  113. foreach ($resultSet as $result) {
  114. // 获取关联外键列表
  115. if (isset($result->$localKey)) {
  116. $range[] = $result->$localKey;
  117. }
  118. }
  119. if (!empty($range)) {
  120. $data = $this->eagerlyWhere($this, [
  121. $foreignKey => [
  122. 'in',
  123. $range,
  124. ],
  125. ], $foreignKey, $relation, $subRelation, $closure);
  126. // 关联属性名
  127. $attr = Loader::parseName($relation);
  128. // 关联数据封装
  129. foreach ($resultSet as $result) {
  130. // 关联模型
  131. if (!isset($data[$result->$localKey])) {
  132. $relationModel = null;
  133. } else {
  134. $relationModel = $data[$result->$localKey];
  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. $data = $this->eagerlyWhere($this, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure);
  162. // 关联模型
  163. if (!isset($data[$result->$localKey])) {
  164. $relationModel = null;
  165. } else {
  166. $relationModel = $data[$result->$localKey];
  167. $relationModel->setParent(clone $result);
  168. $relationModel->isUpdate(true);
  169. }
  170. if (!empty($this->bindAttr)) {
  171. // 绑定关联属性
  172. $this->bindAttr($relationModel, $result, $this->bindAttr);
  173. } else {
  174. $result->setRelation(Loader::parseName($relation), $relationModel);
  175. }
  176. }
  177. }