BelongsToMany.php 18 KB

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