Relation.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types = 1);
  12. namespace think\model;
  13. use Closure;
  14. use ReflectionFunction;
  15. use think\db\BaseQuery as Query;
  16. use think\db\exception\DbException as Exception;
  17. use think\Model;
  18. /**
  19. * 模型关联基础类
  20. * @package think\model
  21. * @mixin Query
  22. */
  23. abstract class Relation
  24. {
  25. /**
  26. * 父模型对象
  27. * @var Model
  28. */
  29. protected $parent;
  30. /**
  31. * 当前关联的模型类名
  32. * @var string
  33. */
  34. protected $model;
  35. /**
  36. * 关联模型查询对象
  37. * @var Query
  38. */
  39. protected $query;
  40. /**
  41. * 关联表外键
  42. * @var string
  43. */
  44. protected $foreignKey;
  45. /**
  46. * 关联表主键
  47. * @var string
  48. */
  49. protected $localKey;
  50. /**
  51. * 是否执行关联基础查询
  52. * @var bool
  53. */
  54. protected $baseQuery;
  55. /**
  56. * 是否为自关联
  57. * @var bool
  58. */
  59. protected $selfRelation = false;
  60. /**
  61. * 关联数据数量限制
  62. * @var int
  63. */
  64. protected $withLimit;
  65. /**
  66. * 关联数据字段限制
  67. * @var array
  68. */
  69. protected $withField;
  70. /**
  71. * 排除关联数据字段
  72. * @var array
  73. */
  74. protected $withoutField;
  75. /**
  76. * 默认数据
  77. * @var mixed
  78. */
  79. protected $default;
  80. /**
  81. * 获取关联的所属模型
  82. * @access public
  83. * @return Model
  84. */
  85. public function getParent(): Model
  86. {
  87. return $this->parent;
  88. }
  89. /**
  90. * 获取当前的关联模型类的Query实例
  91. * @access public
  92. * @return Query
  93. */
  94. public function getQuery()
  95. {
  96. return $this->query;
  97. }
  98. /**
  99. * 获取关联表外键
  100. * @access public
  101. * @return string
  102. */
  103. public function getForeignKey()
  104. {
  105. return $this->foreignKey;
  106. }
  107. /**
  108. * 获取关联表主键
  109. * @access public
  110. * @return string
  111. */
  112. public function getLocalKey()
  113. {
  114. return $this->localKey;
  115. }
  116. /**
  117. * 获取当前的关联模型类的实例
  118. * @access public
  119. * @return Model
  120. */
  121. public function getModel(): Model
  122. {
  123. return $this->query->getModel();
  124. }
  125. /**
  126. * 当前关联是否为自关联
  127. * @access public
  128. * @return bool
  129. */
  130. public function isSelfRelation(): bool
  131. {
  132. return $this->selfRelation;
  133. }
  134. /**
  135. * 封装关联数据集
  136. * @access public
  137. * @param array $resultSet 数据集
  138. * @param Model $parent 父模型
  139. * @return mixed
  140. */
  141. protected function resultSetBuild(array $resultSet, Model $parent = null)
  142. {
  143. return (new $this->model)->toCollection($resultSet)->setParent($parent);
  144. }
  145. protected function getQueryFields(string $model)
  146. {
  147. $fields = $this->query->getOptions('field');
  148. return $this->getRelationQueryFields($fields, $model);
  149. }
  150. protected function getRelationQueryFields($fields, string $model)
  151. {
  152. if (empty($fields) || '*' == $fields) {
  153. return $model . '.*';
  154. }
  155. if (is_string($fields)) {
  156. $fields = explode(',', $fields);
  157. }
  158. foreach ($fields as &$field) {
  159. if (false === strpos($field, '.')) {
  160. $field = $model . '.' . $field;
  161. }
  162. }
  163. return $fields;
  164. }
  165. protected function getQueryWhere(array &$where, string $relation): void
  166. {
  167. foreach ($where as $key => &$val) {
  168. if (is_string($key)) {
  169. $where[] = [false === strpos($key, '.') ? $relation . '.' . $key : $key, '=', $val];
  170. unset($where[$key]);
  171. } elseif (isset($val[0]) && false === strpos($val[0], '.')) {
  172. $val[0] = $relation . '.' . $val[0];
  173. }
  174. }
  175. }
  176. /**
  177. * 限制关联数据的数量
  178. * @access public
  179. * @param int $limit 关联数量限制
  180. * @return $this
  181. */
  182. public function withLimit(int $limit)
  183. {
  184. $this->withLimit = $limit;
  185. return $this;
  186. }
  187. /**
  188. * 限制关联数据的字段
  189. * @access public
  190. * @param array|string $field 关联字段限制
  191. * @return $this
  192. */
  193. public function withField($field)
  194. {
  195. if (is_string($field)) {
  196. $field = array_map('trim', explode(',', $field));
  197. }
  198. $this->withField = $field;
  199. return $this;
  200. }
  201. /**
  202. * 排除关联数据的字段
  203. * @access public
  204. * @param array|string $field 关联字段限制
  205. * @return $this
  206. */
  207. public function withoutField($field)
  208. {
  209. if (is_string($field)) {
  210. $field = array_map('trim', explode(',', $field));
  211. }
  212. $this->withoutField = $field;
  213. return $this;
  214. }
  215. /**
  216. * 设置关联数据不存在的时候默认值
  217. * @access public
  218. * @param mixed $data 默认值
  219. * @return $this
  220. */
  221. public function withDefault($data = null)
  222. {
  223. $this->default = $data;
  224. return $this;
  225. }
  226. /**
  227. * 获取关联数据默认值
  228. * @access protected
  229. * @return mixed
  230. */
  231. protected function getDefaultModel()
  232. {
  233. if (is_array($this->default)) {
  234. $model = (new $this->model)->data($this->default);
  235. } elseif ($this->default instanceof Closure) {
  236. $closure = $this->default;
  237. $model = new $this->model;
  238. $closure($model);
  239. } else {
  240. $model = $this->default;
  241. }
  242. return $model;
  243. }
  244. /**
  245. * 判断闭包的参数类型
  246. * @access protected
  247. * @return mixed
  248. */
  249. protected function getClosureType(Closure $closure)
  250. {
  251. $reflect = new ReflectionFunction($closure);
  252. $params = $reflect->getParameters();
  253. if (!empty($params)) {
  254. $type = $params[0]->getType();
  255. return is_null($type) || Relation::class == $type->getName() ? $this : $this->query;
  256. }
  257. return $this;
  258. }
  259. /**
  260. * 执行基础查询(仅执行一次)
  261. * @access protected
  262. * @return void
  263. */
  264. protected function baseQuery(): void
  265. {}
  266. public function __call($method, $args)
  267. {
  268. if ($this->query) {
  269. // 执行基础查询
  270. $this->baseQuery();
  271. $result = call_user_func_array([$this->query, $method], $args);
  272. return $result === $this->query ? $this : $result;
  273. }
  274. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  275. }
  276. }