BelongsToMany.php 20 KB

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