BelongsToMany.php 19 KB

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