BelongsToMany.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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\relation;
  12. use think\Collection;
  13. use think\db\Query;
  14. use think\Exception;
  15. use think\Loader;
  16. use think\Model;
  17. use think\model\Pivot;
  18. use think\model\Relation;
  19. use think\Paginator;
  20. class BelongsToMany extends Relation
  21. {
  22. // 中间表表名
  23. protected $middle;
  24. // 中间表模型名称
  25. protected $pivotName;
  26. // 中间表模型对象
  27. protected $pivot;
  28. /**
  29. * 构造函数
  30. * @access public
  31. * @param Model $parent 上级模型对象
  32. * @param string $model 模型名
  33. * @param string $table 中间表名
  34. * @param string $foreignKey 关联模型外键
  35. * @param string $localKey 当前模型关联键
  36. */
  37. public function __construct(Model $parent, $model, $table, $foreignKey, $localKey)
  38. {
  39. $this->parent = $parent;
  40. $this->model = $model;
  41. $this->foreignKey = $foreignKey;
  42. $this->localKey = $localKey;
  43. if (false !== strpos($table, '\\')) {
  44. $this->pivotName = $table;
  45. $this->middle = basename(str_replace('\\', '/', $table));
  46. } else {
  47. $this->middle = $table;
  48. }
  49. $this->query = (new $model)->db();
  50. $this->pivot = $this->newPivot();
  51. }
  52. /**
  53. * 设置中间表模型
  54. * @param $pivot
  55. * @return $this
  56. */
  57. public function pivot($pivot)
  58. {
  59. $this->pivotName = $pivot;
  60. return $this;
  61. }
  62. /**
  63. * 实例化中间表模型
  64. * @param $data
  65. * @return Pivot
  66. * @throws Exception
  67. */
  68. protected function newPivot($data = [])
  69. {
  70. $class = $this->pivotName ?: '\\think\\model\\Pivot';
  71. $pivot = new $class($this->parent, $data, $this->middle);
  72. if ($pivot instanceof Pivot) {
  73. return $pivot;
  74. } else {
  75. throw new Exception('pivot model must extends: \think\model\Pivot');
  76. }
  77. }
  78. /**
  79. * 合成中间表模型
  80. * @param array|Collection|Paginator $models
  81. */
  82. protected function hydratePivot($models)
  83. {
  84. foreach ($models as $model) {
  85. $pivot = [];
  86. foreach ($model->getData() as $key => $val) {
  87. if (strpos($key, '__')) {
  88. list($name, $attr) = explode('__', $key, 2);
  89. if ('pivot' == $name) {
  90. $pivot[$attr] = $val;
  91. unset($model->$key);
  92. }
  93. }
  94. }
  95. $model->setRelation('pivot', $this->newPivot($pivot));
  96. }
  97. }
  98. /**
  99. * 创建关联查询Query对象
  100. * @return Query
  101. */
  102. protected function buildQuery()
  103. {
  104. $foreignKey = $this->foreignKey;
  105. $localKey = $this->localKey;
  106. $pk = $this->parent->getPk();
  107. // 关联查询
  108. $condition['pivot.' . $localKey] = $this->parent->$pk;
  109. return $this->belongsToManyQuery($foreignKey, $localKey, $condition);
  110. }
  111. /**
  112. * 延迟获取关联数据
  113. * @param string $subRelation 子关联名
  114. * @param \Closure $closure 闭包查询条件
  115. * @return false|\PDOStatement|string|\think\Collection
  116. */
  117. public function getRelation($subRelation = '', $closure = null)
  118. {
  119. if ($closure) {
  120. call_user_func_array($closure, [ & $this->query]);
  121. }
  122. $result = $this->buildQuery()->relation($subRelation)->select();
  123. $this->hydratePivot($result);
  124. return $result;
  125. }
  126. /**
  127. * 重载select方法
  128. * @param null $data
  129. * @return false|\PDOStatement|string|Collection
  130. */
  131. public function select($data = null)
  132. {
  133. $result = $this->buildQuery()->select($data);
  134. $this->hydratePivot($result);
  135. return $result;
  136. }
  137. /**
  138. * 重载paginate方法
  139. * @param null $listRows
  140. * @param bool $simple
  141. * @param array $config
  142. * @return Paginator
  143. */
  144. public function paginate($listRows = null, $simple = false, $config = [])
  145. {
  146. $result = $this->buildQuery()->paginate($listRows, $simple, $config);
  147. $this->hydratePivot($result);
  148. return $result;
  149. }
  150. /**
  151. * 重载find方法
  152. * @param null $data
  153. * @return array|false|\PDOStatement|string|Model
  154. */
  155. public function find($data = null)
  156. {
  157. $result = $this->buildQuery()->find($data);
  158. if ($result) {
  159. $this->hydratePivot([$result]);
  160. }
  161. return $result;
  162. }
  163. /**
  164. * 查找多条记录 如果不存在则抛出异常
  165. * @access public
  166. * @param array|string|Query|\Closure $data
  167. * @return array|\PDOStatement|string|Model
  168. */
  169. public function selectOrFail($data = null)
  170. {
  171. return $this->failException(true)->select($data);
  172. }
  173. /**
  174. * 查找单条记录 如果不存在则抛出异常
  175. * @access public
  176. * @param array|string|Query|\Closure $data
  177. * @return array|\PDOStatement|string|Model
  178. */
  179. public function findOrFail($data = null)
  180. {
  181. return $this->failException(true)->find($data);
  182. }
  183. /**
  184. * 根据关联条件查询当前模型
  185. * @access public
  186. * @param string $operator 比较操作符
  187. * @param integer $count 个数
  188. * @param string $id 关联表的统计字段
  189. * @param string $joinType JOIN类型
  190. * @return Query
  191. */
  192. public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
  193. {
  194. return $this->parent;
  195. }
  196. /**
  197. * 根据关联条件查询当前模型
  198. * @access public
  199. * @param mixed $where 查询条件(数组或者闭包)
  200. * @return Query
  201. * @throws Exception
  202. */
  203. public function hasWhere($where = [])
  204. {
  205. throw new Exception('relation not support: hasWhere');
  206. }
  207. /**
  208. * 设置中间表的查询条件
  209. * @param $field
  210. * @param null $op
  211. * @param null $condition
  212. * @return $this
  213. */
  214. public function wherePivot($field, $op = null, $condition = null)
  215. {
  216. $field = 'pivot.' . $field;
  217. $this->query->where($field, $op, $condition);
  218. return $this;
  219. }
  220. /**
  221. * 预载入关联查询(数据集)
  222. * @access public
  223. * @param array $resultSet 数据集
  224. * @param string $relation 当前关联名
  225. * @param string $subRelation 子关联名
  226. * @param \Closure $closure 闭包
  227. * @return void
  228. */
  229. public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)
  230. {
  231. $localKey = $this->localKey;
  232. $foreignKey = $this->foreignKey;
  233. $pk = $resultSet[0]->getPk();
  234. $range = [];
  235. foreach ($resultSet as $result) {
  236. // 获取关联外键列表
  237. if (isset($result->$pk)) {
  238. $range[] = $result->$pk;
  239. }
  240. }
  241. if (!empty($range)) {
  242. // 查询关联数据
  243. $data = $this->eagerlyManyToMany([
  244. 'pivot.' . $localKey => [
  245. 'in',
  246. $range,
  247. ],
  248. ], $relation, $subRelation);
  249. // 关联属性名
  250. $attr = Loader::parseName($relation);
  251. // 关联数据封装
  252. foreach ($resultSet as $result) {
  253. if (!isset($data[$result->$pk])) {
  254. $data[$result->$pk] = [];
  255. }
  256. $result->setRelation($attr, $this->resultSetBuild($data[$result->$pk]));
  257. }
  258. }
  259. }
  260. /**
  261. * 预载入关联查询(单个数据)
  262. * @access public
  263. * @param Model $result 数据对象
  264. * @param string $relation 当前关联名
  265. * @param string $subRelation 子关联名
  266. * @param \Closure $closure 闭包
  267. * @return void
  268. */
  269. public function eagerlyResult(&$result, $relation, $subRelation, $closure)
  270. {
  271. $pk = $result->getPk();
  272. if (isset($result->$pk)) {
  273. $pk = $result->$pk;
  274. // 查询管理数据
  275. $data = $this->eagerlyManyToMany(['pivot.' . $this->localKey => $pk], $relation, $subRelation);
  276. // 关联数据封装
  277. if (!isset($data[$pk])) {
  278. $data[$pk] = [];
  279. }
  280. $result->setRelation(Loader::parseName($relation), $this->resultSetBuild($data[$pk]));
  281. }
  282. }
  283. /**
  284. * 关联统计
  285. * @access public
  286. * @param Model $result 数据对象
  287. * @param \Closure $closure 闭包
  288. * @return integer
  289. */
  290. public function relationCount($result, $closure)
  291. {
  292. $pk = $result->getPk();
  293. $count = 0;
  294. if (isset($result->$pk)) {
  295. $pk = $result->$pk;
  296. $count = $this->belongsToManyQuery($this->foreignKey, $this->localKey, ['pivot.' . $this->localKey => $pk])->count();
  297. }
  298. return $count;
  299. }
  300. /**
  301. * 获取关联统计子查询
  302. * @access public
  303. * @param \Closure $closure 闭包
  304. * @return string
  305. */
  306. public function getRelationCountQuery($closure)
  307. {
  308. return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [
  309. 'pivot.' . $this->localKey => [
  310. 'exp',
  311. '=' . $this->parent->getTable() . '.' . $this->parent->getPk(),
  312. ],
  313. ])->fetchSql()->count();
  314. }
  315. /**
  316. * 多对多 关联模型预查询
  317. * @access public
  318. * @param array $where 关联预查询条件
  319. * @param string $relation 关联名
  320. * @param string $subRelation 子关联
  321. * @return array
  322. */
  323. protected function eagerlyManyToMany($where, $relation, $subRelation = '')
  324. {
  325. // 预载入关联查询 支持嵌套预载入
  326. $list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)->with($subRelation)->select();
  327. // 组装模型数据
  328. $data = [];
  329. foreach ($list as $set) {
  330. $pivot = [];
  331. foreach ($set->getData() as $key => $val) {
  332. if (strpos($key, '__')) {
  333. list($name, $attr) = explode('__', $key, 2);
  334. if ('pivot' == $name) {
  335. $pivot[$attr] = $val;
  336. unset($set->$key);
  337. }
  338. }
  339. }
  340. $set->setRelation('pivot', $this->newPivot($pivot));
  341. $data[$pivot[$this->localKey]][] = $set;
  342. }
  343. return $data;
  344. }
  345. /**
  346. * BELONGS TO MANY 关联查询
  347. * @access public
  348. * @param string $foreignKey 关联模型关联键
  349. * @param string $localKey 当前模型关联键
  350. * @param array $condition 关联查询条件
  351. * @return Query
  352. */
  353. protected function belongsToManyQuery($foreignKey, $localKey, $condition = [])
  354. {
  355. // 关联查询封装
  356. $tableName = $this->query->getTable();
  357. $table = $this->pivot->getTable();
  358. $fields = $this->getQueryFields($tableName);
  359. $query = $this->query->field($fields)
  360. ->field(true, false, $table, 'pivot', 'pivot__');
  361. if (empty($this->baseQuery)) {
  362. $relationFk = $this->query->getPk();
  363. $query->join($table . ' pivot', 'pivot.' . $foreignKey . '=' . $tableName . '.' . $relationFk)
  364. ->where($condition);
  365. }
  366. return $query;
  367. }
  368. /**
  369. * 保存(新增)当前关联数据对象
  370. * @access public
  371. * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键
  372. * @param array $pivot 中间表额外数据
  373. * @return integer
  374. */
  375. public function save($data, array $pivot = [])
  376. {
  377. // 保存关联表/中间表数据
  378. return $this->attach($data, $pivot);
  379. }
  380. /**
  381. * 批量保存当前关联数据对象
  382. * @access public
  383. * @param array $dataSet 数据集
  384. * @param array $pivot 中间表额外数据
  385. * @param bool $samePivot 额外数据是否相同
  386. * @return integer
  387. */
  388. public function saveAll(array $dataSet, array $pivot = [], $samePivot = false)
  389. {
  390. $result = false;
  391. foreach ($dataSet as $key => $data) {
  392. if (!$samePivot) {
  393. $pivotData = isset($pivot[$key]) ? $pivot[$key] : [];
  394. } else {
  395. $pivotData = $pivot;
  396. }
  397. $result = $this->attach($data, $pivotData);
  398. }
  399. return $result;
  400. }
  401. /**
  402. * 附加关联的一个中间表数据
  403. * @access public
  404. * @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
  405. * @param array $pivot 中间表额外数据
  406. * @return array|Pivot
  407. * @throws Exception
  408. */
  409. public function attach($data, $pivot = [])
  410. {
  411. if (is_array($data)) {
  412. if (key($data) === 0) {
  413. $id = $data;
  414. } else {
  415. // 保存关联表数据
  416. $model = new $this->model;
  417. $model->save($data);
  418. $id = $model->getLastInsID();
  419. }
  420. } elseif (is_numeric($data) || is_string($data)) {
  421. // 根据关联表主键直接写入中间表
  422. $id = $data;
  423. } elseif ($data instanceof Model) {
  424. // 根据关联表主键直接写入中间表
  425. $relationFk = $data->getPk();
  426. $id = $data->$relationFk;
  427. }
  428. if ($id) {
  429. // 保存中间表数据
  430. $pk = $this->parent->getPk();
  431. $pivot[$this->localKey] = $this->parent->$pk;
  432. $ids = (array) $id;
  433. foreach ($ids as $id) {
  434. $pivot[$this->foreignKey] = $id;
  435. $this->pivot->insert($pivot, true);
  436. $result[] = $this->newPivot($pivot);
  437. }
  438. if (count($result) == 1) {
  439. // 返回中间表模型对象
  440. $result = $result[0];
  441. }
  442. return $result;
  443. } else {
  444. throw new Exception('miss relation data');
  445. }
  446. }
  447. /**
  448. * 解除关联的一个中间表数据
  449. * @access public
  450. * @param integer|array $data 数据 可以使用关联对象的主键
  451. * @param bool $relationDel 是否同时删除关联表数据
  452. * @return integer
  453. */
  454. public function detach($data = null, $relationDel = false)
  455. {
  456. if (is_array($data)) {
  457. $id = $data;
  458. } elseif (is_numeric($data) || is_string($data)) {
  459. // 根据关联表主键直接写入中间表
  460. $id = $data;
  461. } elseif ($data instanceof Model) {
  462. // 根据关联表主键直接写入中间表
  463. $relationFk = $data->getPk();
  464. $id = $data->$relationFk;
  465. }
  466. // 删除中间表数据
  467. $pk = $this->parent->getPk();
  468. $pivot[$this->localKey] = $this->parent->$pk;
  469. if (isset($id)) {
  470. $pivot[$this->foreignKey] = is_array($id) ? ['in', $id] : $id;
  471. }
  472. $this->pivot->where($pivot)->delete();
  473. // 删除关联表数据
  474. if (isset($id) && $relationDel) {
  475. $model = $this->model;
  476. $model::destroy($id);
  477. }
  478. }
  479. /**
  480. * 数据同步
  481. * @param array $ids
  482. * @param bool $detaching
  483. * @return array
  484. */
  485. public function sync($ids, $detaching = true)
  486. {
  487. $changes = [
  488. 'attached' => [],
  489. 'detached' => [],
  490. 'updated' => [],
  491. ];
  492. $pk = $this->parent->getPk();
  493. $current = $this->pivot->where($this->localKey, $this->parent->$pk)
  494. ->column($this->foreignKey);
  495. $records = [];
  496. foreach ($ids as $key => $value) {
  497. if (!is_array($value)) {
  498. $records[$value] = [];
  499. } else {
  500. $records[$key] = $value;
  501. }
  502. }
  503. $detach = array_diff($current, array_keys($records));
  504. if ($detaching && count($detach) > 0) {
  505. $this->detach($detach);
  506. $changes['detached'] = $detach;
  507. }
  508. foreach ($records as $id => $attributes) {
  509. if (!in_array($id, $current)) {
  510. $this->attach($id, $attributes);
  511. $changes['attached'][] = $id;
  512. } elseif (count($attributes) > 0 &&
  513. $this->attach($id, $attributes)
  514. ) {
  515. $changes['updated'][] = $id;
  516. }
  517. }
  518. return $changes;
  519. }
  520. /**
  521. * 执行基础查询(进执行一次)
  522. * @access protected
  523. * @return void
  524. */
  525. protected function baseQuery()
  526. {
  527. if (empty($this->baseQuery) && $this->parent->getData()) {
  528. $pk = $this->parent->getPk();
  529. $table = $this->pivot->getTable();
  530. $this->query->join($table . ' pivot', 'pivot.' . $this->foreignKey . '=' . $this->query->getTable() . '.' . $this->query->getPk())->where('pivot.' . $this->localKey, $this->parent->$pk);
  531. $this->baseQuery = true;
  532. }
  533. }
  534. }