<?php namespace app\admin\model; use think\Model; class GoodsCate extends Model { //获取分类树 public function get_tree ($pid = 0,$field = ‘*‘){ $list = self::all(function($query)use($pid,$field){ $query->where([‘pid‘=>$pid])->field($field); }); foreach ($list as &$v){ if(self::get([‘pid‘=>$v[‘id‘]])){ $v[‘child‘] = self::get_tree($v[‘id‘],$field); } } return $list; } //根据id获取所有层级父类id public static function get_parent ($id = 0){ $list = self::all(); $arr = array_column($list,‘pid‘,‘id‘); $ids[] = $id; while($arr[$id]) { $id = $arr[$id]; $ids[] = $id; } return $ids; } //根据id获取所有子级id public static function get_child ($id,&$ids=[]){ array_push($ids,$id); $list = self::where(‘pid‘,$id)->column(‘id‘); foreach ($list as $v) { self::get_child($v,$ids); } return $ids; } //根据id获取当前数据及其子类列表 public function get_list ($id=0,$field=‘*‘,$where){ $info = self::where(‘id‘,$id)->alias(‘goods_cate‘)->field($field)->find(); if($info) { $list[] = $info; } $list = $this->get_data($id,$list,$field,$where); return $list; } public function get_data($id,&$list=[],$field,$where) { $child = self::where(‘pid‘,‘in‘,$id)->alias(‘goods_cate‘)->field($field)->where($where)->select(); if($child) { foreach ($child as $item) { $list[] = $item; self::get_data($item[‘id‘], $list,$field,$where); } } return $list; } }
原文地址:https://www.cnblogs.com/ivy-zheng/p/12146254.html
时间: 2024-10-12 06:32:52