Relation.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * @access public
  73. * @return Model
  74. */
  75. public function getParent(): Model
  76. {
  77. return $this->parent;
  78. }
  79. /**
  80. * 获取当前的关联模型类的Query实例
  81. * @access public
  82. * @return Query
  83. */
  84. public function getQuery()
  85. {
  86. return $this->query;
  87. }
  88. /**
  89. * 获取当前的关联模型类的实例
  90. * @access public
  91. * @return Model
  92. */
  93. public function getModel(): Model
  94. {
  95. return $this->query->getModel();
  96. }
  97. /**
  98. * 当前关联是否为自关联
  99. * @access public
  100. * @return bool
  101. */
  102. public function isSelfRelation(): bool
  103. {
  104. return $this->selfRelation;
  105. }
  106. /**
  107. * 封装关联数据集
  108. * @access public
  109. * @param array $resultSet 数据集
  110. * @param Model $parent 父模型
  111. * @return mixed
  112. */
  113. protected function resultSetBuild(array $resultSet, Model $parent = null)
  114. {
  115. return (new $this->model)->toCollection($resultSet)->setParent($parent);
  116. }
  117. protected function getQueryFields(string $model)
  118. {
  119. $fields = $this->query->getOptions('field');
  120. return $this->getRelationQueryFields($fields, $model);
  121. }
  122. protected function getRelationQueryFields($fields, string $model)
  123. {
  124. if (empty($fields) || '*' == $fields) {
  125. return $model . '.*';
  126. }
  127. if (is_string($fields)) {
  128. $fields = explode(',', $fields);
  129. }
  130. foreach ($fields as &$field) {
  131. if (false === strpos($field, '.')) {
  132. $field = $model . '.' . $field;
  133. }
  134. }
  135. return $fields;
  136. }
  137. protected function getQueryWhere(array &$where, string $relation): void
  138. {
  139. foreach ($where as $key => &$val) {
  140. if (is_string($key)) {
  141. $where[] = [false === strpos($key, '.') ? $relation . '.' . $key : $key, '=', $val];
  142. unset($where[$key]);
  143. } elseif (isset($val[0]) && false === strpos($val[0], '.')) {
  144. $val[0] = $relation . '.' . $val[0];
  145. }
  146. }
  147. }
  148. /**
  149. * 更新数据
  150. * @access public
  151. * @param array $data 更新数据
  152. * @return integer
  153. */
  154. public function update(array $data = []): int
  155. {
  156. return $this->query->update($data);
  157. }
  158. /**
  159. * 删除记录
  160. * @access public
  161. * @param mixed $data 表达式 true 表示强制删除
  162. * @return int
  163. * @throws Exception
  164. * @throws PDOException
  165. */
  166. public function delete($data = null): int
  167. {
  168. return $this->query->delete($data);
  169. }
  170. /**
  171. * 限制关联数据的数量
  172. * @access public
  173. * @param int $limit 关联数量限制
  174. * @return $this
  175. */
  176. public function withLimit(int $limit)
  177. {
  178. $this->withLimit = $limit;
  179. return $this;
  180. }
  181. /**
  182. * 限制关联数据的字段
  183. * @access public
  184. * @param array $field 关联字段限制
  185. * @return $this
  186. */
  187. public function withField(array $field)
  188. {
  189. $this->withField = $field;
  190. return $this;
  191. }
  192. /**
  193. * 判断闭包的参数类型
  194. * @access protected
  195. * @return mixed
  196. */
  197. protected function getClosureType(Closure $closure)
  198. {
  199. $reflect = new ReflectionFunction($closure);
  200. $params = $reflect->getParameters();
  201. if (!empty($params)) {
  202. $type = $params[0]->getType();
  203. return Relation::class == $type || is_null($type) ? $this : $this->query;
  204. }
  205. return $this;
  206. }
  207. /**
  208. * 执行基础查询(仅执行一次)
  209. * @access protected
  210. * @return void
  211. */
  212. protected function baseQuery(): void
  213. {}
  214. public function __call($method, $args)
  215. {
  216. if ($this->query) {
  217. // 执行基础查询
  218. $this->baseQuery();
  219. $result = call_user_func_array([$this->query, $method], $args);
  220. return $result === $this->query ? $this : $result;
  221. }
  222. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  223. }
  224. }