Relation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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;
  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 $option;
  35. // 基础查询
  36. protected $baseQuery;
  37. /**
  38. * 获取关联的所属模型
  39. * @access public
  40. * @return Model
  41. */
  42. public function getParent()
  43. {
  44. return $this->parent;
  45. }
  46. /**
  47. * 获取当前的关联模型类
  48. * @access public
  49. * @return string
  50. */
  51. public function getModel()
  52. {
  53. return $this->model;
  54. }
  55. /**
  56. * 获取关联的查询对象
  57. * @access public
  58. * @return Query
  59. */
  60. public function getQuery()
  61. {
  62. return $this->query;
  63. }
  64. /**
  65. * 封装关联数据集
  66. * @access public
  67. * @param array $resultSet 数据集
  68. * @return mixed
  69. */
  70. protected function resultSetBuild($resultSet)
  71. {
  72. return (new $this->model)->toCollection($resultSet);
  73. }
  74. /**
  75. * 移除关联查询参数
  76. * @access public
  77. * @return $this
  78. */
  79. public function removeOption()
  80. {
  81. $this->query->removeOption();
  82. return $this;
  83. }
  84. /**
  85. * 执行基础查询(进执行一次)
  86. * @access protected
  87. * @return void
  88. */
  89. abstract protected function baseQuery();
  90. public function __call($method, $args)
  91. {
  92. if ($this->query) {
  93. // 执行基础查询
  94. $this->baseQuery();
  95. $result = call_user_func_array([$this->query, $method], $args);
  96. if ($result instanceof Query) {
  97. $this->option = $result->getOptions();
  98. return $this;
  99. } else {
  100. $this->option = [];
  101. $this->baseQuery = false;
  102. return $result;
  103. }
  104. } else {
  105. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  106. }
  107. }
  108. }