PHP通过数据库递归的方式查找当前分类ID的所有上级ID

/**
     * 通过数据库递归的方式查找当前分类ID的所有上级ID
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function findAllParents($uid, $id): array
    {
        static $ids = [];
        $info = Db::name('表')->where('uid', $uid)->where('id', $id)->find();
        if (!empty($info)) {
            $ids[] = $info['id'];
            $this->findAllParents($uid, $info['pid']);
        }
        return $ids;
    }

 

posted @ 2022-12-23 23:13  _迷途  阅读(91)  评论(0编辑  收藏  举报