Relation.php 6.9 KB

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