Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. <?php
  2. /**
  3. * ecshop 无限级上下关系描述
  4. * CREATE TABLE `ecs_parent` (
  5. * `parent_id` int(11) UNSIGNED NOT NULL DEFAULT 0 ,
  6. * `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 ,
  7. * `level` int(11) UNSIGNED NOT NULL DEFAULT 1 ,
  8. * UNIQUE INDEX `parent_id` (`parent_id`, `user_id`) USING BTREE
  9. * )
  10. * ENGINE=InnoDB
  11. * DEFAULT CHARACTER SET=latin1 COLLATE=latin1_swedish_ci
  12. * ROW_FORMAT=COMPACT;
  13. */
  14. require_once ('MysqliDb.php');
  15. $dbi = new MysqliDb ([
  16. 'host' => 'localhost',
  17. 'username' => 'root',
  18. 'password' => 'root',
  19. 'db'=> 'test',
  20. 'prefix' => 'ecs_'
  21. ]);
  22.  
  23. $users = [
  24. 1 => 'a',
  25. 2 => 'b',
  26. 3 => 'c',
  27. 4 => 'd',
  28. 5 => 'e',
  29. 6 => 'f',
  30. 7 => 'g',
  31. 8 => 'h',
  32. ];
  33.  
  34. init();
  35. //delete_parent(3);
  36. //add_parent(6, 3);
  37. //update_parent(3, 6);
  38. //show parents
  39. $parents = $dbi->orderBy('parent_id', 'asc')->orderBy('level', 'asc')->get('parent');
  40. foreach ($parents as $parent) {
  41. echo $users[ $parent['parent_id'] ] . ' -> ' . $users[ $parent['user_id'] ] . ', ' . $parent['level'] . '级'.PHP_EOL;
  42. }
  43.  
  44. function init(){
  45. $GLOBALS['dbi']->rawQuery('truncate ecs_parent');
  46. //a->b
  47. add_parent(1, 2);
  48. //b->c
  49. add_parent(2, 3);
  50. //c->d
  51. add_parent(3, 4);
  52. //d->e
  53. add_parent(4, 5);
  54. //a->f
  55. add_parent(1, 6);
  56. //f->g
  57. add_parent(6, 7);
  58. //g->h
  59. add_parent(7, 8);
  60. }
  61. /**
  62. * 新增上下级关系
  63. * @param integer $parent_id 上级id
  64. * @param integer $user_id 下级id
  65. */
  66. function add_parent($parent_id, $user_id){
  67. $GLOBALS['dbi']->startTransaction();
  68. try{
  69. //建立跟上级的关系
  70. $GLOBALS['dbi']->insert('parent', ['parent_id' => $parent_id, 'user_id' => $user_id, 'level' => 1]);
  71. //如果上级有上级,把我跟每一个上级建立关系
  72. $grandpas = $GLOBALS['dbi']->where('user_id', $parent_id)->get('parent', null, ['parent_id', 'level']);
  73. foreach ($grandpas as $parent){
  74. $GLOBALS['dbi']->insert('parent', ['parent_id' => $parent['parent_id'], 'user_id' => $user_id, 'level' => $parent['level']+1 ]);
  75. }
  76. //如果我有下级,把我所有下级跟我的上级建立关系
  77. $children = $GLOBALS['dbi']->where('parent_id', $user_id)->get('parent', null, ['user_id', 'level']);
  78. foreach ($children as $child){
  79. $GLOBALS['dbi']->insert('parent', ['parent_id' => $parent_id, 'user_id' => $child['user_id'], 'level' => $child['level']+1]);
  80. //还要跟我的上级的所有上级建立关系
  81. foreach ($grandpas as $parent){
  82. $GLOBALS['dbi']->insert('parent', ['parent_id' => $parent['parent_id'], 'user_id' => $child['user_id'], 'level' => $child['level'] + $parent['level'] + 1 ]);
  83. }
  84. }
  85. $GLOBALS['dbi']->commit();
  86. } catch(Exception $e){
  87. $GLOBALS['dbi']->rollback();
  88. }
  89. }
  90. /**
  91. * 更改上级
  92. * @param integer $user_id 用户id
  93. * @param integer $parent_id 新上级id,为0时仅去除原有上级
  94. */
  95. function update_parent($user_id, $parent_id = 0){
  96. delete_parent($user_id);
  97. if($parent_id > 0){
  98. add_parent($parent_id, $user_id);
  99. }
  100. }
  101. /**
  102. * 去除上级
  103. * @param integer $user_id 用户id
  104. */
  105. function delete_parent($user_id){
  106. $GLOBALS['dbi']->startTransaction();
  107. try{
  108. //先跟原来的上级们断掉关系
  109. $GLOBALS['dbi']->where('user_id', $user_id)->delete('parent');
  110. //要断绝跟上级们的关系,还得加上我的下级们
  111. $children = $GLOBALS['dbi']->where('parent_id', $user_id)->get('parent', null, ['user_id', 'level']);
  112. foreach ($children as $child){
  113. //对每一个下级而言,跟“我”的关系不用变,但是在我之上的更远关系应该删除,无论更远的上级是谁
  114. $GLOBALS['dbi']->where('user_id', $child['user_id'])->where('level', ['>' => $child['level']])->delete('parent');
  115. }
  116. $GLOBALS['dbi']->commit();
  117. } catch(Exception $e){
  118. $GLOBALS['dbi']->rollback();
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement