Relation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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;
  12. use think\db\Query;
  13. use think\Exception;
  14. use think\Model;
  15. /**
  16. * Class Relation
  17. * @package think\model
  18. *
  19. * @mixin Query
  20. */
  21. abstract class Relation
  22. {
  23. // 父模型对象
  24. protected $parent;
  25. /** @var Model 当前关联的模型类 */
  26. protected $model;
  27. /** @var Query 关联模型查询对象 */
  28. protected $query;
  29. // 关联表外键
  30. protected $foreignKey;
  31. // 关联表主键
  32. protected $localKey;
  33. // 基础查询
  34. protected $baseQuery;
  35. /**
  36. * 获取关联的所属模型
  37. * @access public
  38. * @return Model
  39. */
  40. public function getParent()
  41. {
  42. return $this->parent;
  43. }
  44. /**
  45. * 获取当前的关联模型类
  46. * @access public
  47. * @return string
  48. */
  49. public function getModel()
  50. {
  51. return $this->model;
  52. }
  53. /**
  54. * 获取关联的查询对象
  55. * @access public
  56. * @return Query
  57. */
  58. public function getQuery()
  59. {
  60. return $this->query;
  61. }
  62. /**
  63. * 封装关联数据集
  64. * @access public
  65. * @param array $resultSet 数据集
  66. * @return mixed
  67. */
  68. protected function resultSetBuild($resultSet)
  69. {
  70. return (new $this->model)->toCollection($resultSet);
  71. }
  72. protected function getQueryFields($model)
  73. {
  74. $fields = $this->query->getOptions('field');
  75. return $this->getRelationQueryFields($fields, $model);
  76. }
  77. protected function getRelationQueryFields($fields, $model)
  78. {
  79. if ($fields) {
  80. if (is_string($fields)) {
  81. $fields = explode(',', $fields);
  82. }
  83. foreach ($fields as &$field) {
  84. if (false === strpos($field, '.')) {
  85. $field = $model . '.' . $field;
  86. }
  87. }
  88. } else {
  89. $fields = $model . '.*';
  90. }
  91. return $fields;
  92. }
  93. /**
  94. * 执行基础查询(仅执行一次)
  95. * @access protected
  96. * @return void
  97. */
  98. protected function baseQuery()
  99. {}
  100. public function __call($method, $args)
  101. {
  102. if ($this->query) {
  103. // 执行基础查询
  104. $this->baseQuery();
  105. $result = call_user_func_array([$this->query, $method], $args);
  106. if ($result instanceof Query) {
  107. return $this;
  108. } else {
  109. $this->baseQuery = false;
  110. return $result;
  111. }
  112. } else {
  113. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  114. }
  115. }
  116. }