Relation.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * @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 $data 更新数据
  175. * @return integer
  176. */
  177. public function update(array $data = []): int
  178. {
  179. return $this->query->update($data);
  180. }
  181. /**
  182. * 删除记录
  183. * @access public
  184. * @param mixed $data 表达式 true 表示强制删除
  185. * @return int
  186. * @throws Exception
  187. * @throws PDOException
  188. */
  189. public function delete($data = null): int
  190. {
  191. return $this->query->delete($data);
  192. }
  193. /**
  194. * 限制关联数据的数量
  195. * @access public
  196. * @param int $limit 关联数量限制
  197. * @return $this
  198. */
  199. public function withLimit(int $limit)
  200. {
  201. $this->withLimit = $limit;
  202. return $this;
  203. }
  204. /**
  205. * 限制关联数据的字段
  206. * @access public
  207. * @param array $field 关联字段限制
  208. * @return $this
  209. */
  210. public function withField(array $field)
  211. {
  212. $this->withField = $field;
  213. return $this;
  214. }
  215. /**
  216. * 排除关联数据的字段
  217. * @access public
  218. * @param array|string $field 关联字段限制
  219. * @return $this
  220. */
  221. public function withoutField($field)
  222. {
  223. if (is_string($field)) {
  224. $field = array_map('trim', explode(',', $field));
  225. }
  226. $this->withoutField = $field;
  227. return $this;
  228. }
  229. /**
  230. * 判断闭包的参数类型
  231. * @access protected
  232. * @return mixed
  233. */
  234. protected function getClosureType(Closure $closure)
  235. {
  236. $reflect = new ReflectionFunction($closure);
  237. $params = $reflect->getParameters();
  238. if (!empty($params)) {
  239. $type = $params[0]->getType();
  240. return is_null($type) || Relation::class == $type->getName() ? $this : $this->query;
  241. }
  242. return $this;
  243. }
  244. /**
  245. * 执行基础查询(仅执行一次)
  246. * @access protected
  247. * @return void
  248. */
  249. protected function baseQuery(): void
  250. {}
  251. public function __call($method, $args)
  252. {
  253. if ($this->query) {
  254. // 执行基础查询
  255. $this->baseQuery();
  256. $result = call_user_func_array([$this->query, $method], $args);
  257. return $result === $this->query ? $this : $result;
  258. }
  259. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  260. }
  261. }