项目中常用到无限级,所以把之前写的循环拿出来整理下,以后遇到省的调试
//无限级查询上级
public function getParent($uid = 0){
if (!$this->find($uid)) {
return [];
}
$pid = $this->where('id',$uid)->value('pid');
if ($pid) {
$this->res[] = $pid;
$this->getParent($pid);
}else return [];
return $this->res;
}
//无限级向上查找最近等级的会员信息
public function getLevelParent($uid = 0,$level = 0){
if (!$this->find($uid)) {
return [];
}
$parent = $this->where('id',$uid)->find();
if ($parent['level'] >= $level) {
return $parent;
}else{
return $this->getLevelParent($parent['pid'],$level);
}
}
//查到无限极代理
protected function getAgent($user_id)
{
$user = U::where('id', $user_id)->lock(true)->find();
if (!$user) {
return [];
}
if($user->isagent){
return $user;
}else{
return $this->getAgent($user->pid);
}
}
//获得伞下所有人
public function getChild($id)
{
$childs = M('member')->where(array('parent_id'=>$id))->field('id')->select();;
if (!$childs) {
return [];
}else{
foreach($childs as $child){
$this->childs[] = $child['id'];
$this->getChild($child['id']);
}
}
return $this->childs;
}
评论回复