一:原理
参考:https://www.cnblogs.com/onew/p/5290186.html
a推广出的a-1,a-2继续推广,得到a-1-1,a-1-2等等
数据库设计思路如下:
用户表中有一个son这么一个字段,这个字段中存放名下所有会员的id,用分号隔开。
这个字段的维护:
比如a-1-1推广出了一个a-1-1-1,此新用户的id是12345,那么给a-1-1 a-1 a这个三个用户son字段内均添加12345这个id,删除一个用户做法一样。
有了这张表就能实现你要的效果。
查一个会员名下所有的会员,只需要读取该会员的son字段即可
查一个会员的上级 怎在数据库中所搜son字段,含有此会员id的都是他的上级会员。
当然,还有一个字段是标记此会员的直接上级会员,这样 一张表就能从任意会员得到整个会员推广树。
字段 id pid name son ...后面省略
id | pid | name | son |
1 | 0 | a | 2,3,4,5,6,7,8,9 |
2 | 1 | a-1 | 4,5,6,7,8,9 |
3 | 1 | a-2 | 0 |
4 | 2 | a-1-1 | 6,7,8,9, |
5 | 2 | a-1-2 | 0 |
6 | 4 | a-1-1-1 | 8,9 |
7 | 4 | a-1-1-2 | 0 |
8 | 6 | a-1-1-1-1 | 9 |
9 | 8 | a-1-1-1-1-1 | 0 |
这个是测试数据
要获取id为7的所有上级,只需在son中找7即可,1 2 4 均是7的上级 4是7的直接上级
要获取id为4的所有下级,直接读取son即可,6 7 8 9 为4的下级
son里面的id号 怎么添加上去?
以9为例,添加9这个用户时肯定是知道他的直接上级是8,因为就是通过8推广得到9的,然后根据8取到8的所有上级,将9添加到8的所有上级的son字段中,包括8也要添加。
二:TP5.1代码:
表设计
/**
* 添加、查询用户上下级有关系
* @Author
* @DateTime 2020-07-24
* @param [type] $user_id int/必填 [用户名ID]
* @param [type] $pid int/必填 [上级ID]
* @return
*/
public function AddUserParentid($user_id, $pid)
{
$up_data=[];$up_data2=[];$up_data_arr=[];
$utime=date('Y-m-d H:i:s',time());
// 启动事务
Db::startTrans();
try {
if (!$user_id) {
throw new \Exception("用户名ID为空!");
}
if (!$pid) {
throw new \Exception("父ID为空!");
}
//先判断用户是否有上级
$rs=Db::name('user_parentid')->field('id')->where('user_id', $user_id)->find();
if ($rs) {
// throw new \Exception("已经存在父关系!");
$userId =$rs['id'];
}else{
//1.添加本身父关系记录
$idata['user_id']=$user_id;
$idata['pid']=$pid;
$userId = Db::name('user_parentid')->insertGetId($idata);
if (!$userId) {
throw new \Exception("添加父关系失败!");
}
}
//2.给user_id直属上级(父)ID增加下级ID($userId)
$p_rs_2=Db::name('user_parentid')->field('id,son')->where('user_id', $pid)->find();
if($p_rs_2){
// \dump($p_rs_2);exit;
// \dump($p_rs_2);
// if (!$p_rs_2) {
// throw new \Exception("没找到父ID对应信息!");
// }
$up_data[0]['id']=$p_rs_2['id'];
$up_data[0]['utime']=$utime;
if(empty($p_rs_2['son'])){
$up_data[0]['son']=$userId;
}else{
$up_data[0]['son']=$p_rs_2['son'].','.$userId;
}
//3.给user_id上级上级的(除了直属上级)所有父ID增加下级ID($userId)
$p_rs_3=Db::name('user_parentid')->field('id,son')->where('FIND_IN_SET(:id,son)',['id' => $p_rs_2['id']])->select();
// \dump($p_rs_3);exit;
if($p_rs_3){
foreach($p_rs_3 as $k=> $v){
$up_data2[$k]['id']=$v['id'];
$up_data2[$k]['utime']=$utime;
if(empty($v['son'])){
$up_data2[$k]['son']=$userId;
}else{
$up_data2[$k]['son']=$v['son'].','.$userId;
}
}
}
// \dump($up_data);
// \dump($up_data2);
$up_data_arr= array_values(array_merge($up_data,$up_data2));
// \dump($up_data_arr);exit;
// \dump($up_data_arr);exit;
// $up_data_arr= $up_data2;
if($up_data_arr){
// \dump($up_data_arr);exit;
foreach($up_data_arr as $key=>$val){
$urs=Db::name('user_parentid')->where('id',$val['id'])->setField('son',$val['son']);
// echo Db::name('user_parentid')->getLastSql();
// \dump($urs);exit;
if (!$urs) {
throw new \Exception("更新下级失败!");
}
}
}
}
// \dump(111);exit;
$code=200;$msg="成功";
// 提交事务
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
$code=-200;
$msg=$e->getMessage();
}
return array('code'=>$code,'msg'=>$msg);
}
三、调用:
//输入:1,51
//输入:2,1
//输入:3,1
.....
//输入:9,8
$AddUserParentid = model('index/UserModel')->AddUserParentid(9,8);
dump($AddUserParentid);exit;