BelongsTo.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. */
  25. public function __construct(Model $parent, $model, $foreignKey, $localKey, $joinType = 'INNER')
  26. {
  27. $this->parent = $parent;
  28. $this->model = $model;
  29. $this->foreignKey = $foreignKey;
  30. $this->localKey = $localKey;
  31. $this->joinType = $joinType;
  32. $this->query = (new $model)->db();
  33. }
  34. /**
  35. * 延迟获取关联数据
  36. * @param string $subRelation 子关联名
  37. * @param \Closure $closure 闭包查询条件
  38. * @access public
  39. * @return array|false|\PDOStatement|string|Model
  40. */
  41. public function getRelation($subRelation = '', $closure = null)
  42. {
  43. $foreignKey = $this->foreignKey;
  44. if ($closure) {
  45. call_user_func_array($closure, [ & $this->query]);
  46. }
  47. return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find();
  48. }
  49. /**
  50. * 预载入关联查询(数据集)
  51. * @access public
  52. * @param array $resultSet 数据集
  53. * @param string $relation 当前关联名
  54. * @param string $subRelation 子关联名
  55. * @param \Closure $closure 闭包
  56. * @return void
  57. */
  58. protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure)
  59. {
  60. $localKey = $this->localKey;
  61. $foreignKey = $this->foreignKey;
  62. $range = [];
  63. foreach ($resultSet as $result) {
  64. // 获取关联外键列表
  65. if (isset($result->$foreignKey)) {
  66. $range[] = $result->$foreignKey;
  67. }
  68. }
  69. if (!empty($range)) {
  70. $data = $this->eagerlyWhere($this, [
  71. $localKey => [
  72. 'in',
  73. $range,
  74. ],
  75. ], $localKey, $relation, $subRelation, $closure);
  76. // 关联属性名
  77. $attr = Loader::parseName($relation);
  78. // 关联数据封装
  79. foreach ($resultSet as $result) {
  80. // 关联模型
  81. if (!isset($data[$result->$localKey])) {
  82. $relationModel = null;
  83. } else {
  84. $relationModel = $data[$result->$localKey];
  85. }
  86. if ($relationModel && !empty($this->bindAttr)) {
  87. // 绑定关联属性
  88. $this->bindAttr($relationModel, $result, $this->bindAttr);
  89. }
  90. // 设置关联属性
  91. $result->setAttr($attr, $relationModel);
  92. }
  93. }
  94. }
  95. /**
  96. * 预载入关联查询(数据)
  97. * @access public
  98. * @param Model $result 数据对象
  99. * @param string $relation 当前关联名
  100. * @param string $subRelation 子关联名
  101. * @param \Closure $closure 闭包
  102. * @return void
  103. */
  104. protected function eagerlyOne(&$result, $relation, $subRelation, $closure)
  105. {
  106. $localKey = $this->localKey;
  107. $foreignKey = $this->foreignKey;
  108. $data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
  109. // 关联模型
  110. if (!isset($data[$result->$localKey])) {
  111. $relationModel = null;
  112. } else {
  113. $relationModel = $data[$result->$localKey];
  114. }
  115. if ($relationModel && !empty($this->bindAttr)) {
  116. // 绑定关联属性
  117. $this->bindAttr($relationModel, $result, $this->bindAttr);
  118. }
  119. // 设置关联属性
  120. $result->setAttr(Loader::parseName($relation), $relationModel);
  121. }
  122. }