\App\Http\HelpersAncestorsPath

祖级路径前缀区间工具

职责:祖级路径前缀区间算法(原在四处手写复制,此处收敛为单一来源)—— 给定某节点的祖先路径与自身 id,计算恰好覆盖其全部后代 ancestors 字符串的区间 [lower, upper),使后代范围过滤能走 idx_*_ancestors 索引; 并提供子节点路径拼接与级联重算(组织/地区两服务共用)。 只做区间计算,不做树遍历(树构建见 ArrHelper::buildTree)。

Summary

Methods
Properties
Constants
prefixRange()
childPath()
cascadeUpdate()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

prefixRange()

prefixRange(string  $ancestors, int  $id) : array

子树前缀区间下界与上界(不含)。

ancestors 形如 '0/1/5/':某节点后代的 ancestors 均以「该节点 ancestors + 该节点 id + '/'」开头, 区间 [prefix, upper) 恰好覆盖该前缀全部字符串('/' 的下一个 ASCII 字符是 '0',故上界取 「去掉末尾 '/' 后补 '0'」)。本库 collation 非 C,LIKE 'prefix%' 用不上普通 btree 索引, 故改用范围比较以命中 idx_*_ancestors 索引。

Parameters

string $ancestors
int $id

Returns

array —

childPath()

childPath(?string  $parentAncestors, ?int  $parentId) : string

子节点祖级路径:父为根 → '0/';否则 parent.ancestors + parent.id + '/'。 (父节点查询与防环校验由调用方按其模型执行,此处只做路径拼接单源)

Parameters

?string $parentAncestors
?int $parentId

Returns

string —

cascadeUpdate()

cascadeUpdate(string  $table, string  $oldAncestors, int  $selfId, string  $newAncestors, int  $levelDelta) : void

级联重算后代 ancestors(子树前缀区间批量 UPDATE,走索引): SET ancestors = 新前缀 || substr(ancestors, length(旧前缀)+1) WHERE ancestors ∈ [prefix, upper) 可选 level 平移($levelDelta ≠ 0 时同步 level = level + delta;地区树用)。 批量 UPDATE 不触发模型事件——缓存失效由调用方在事务提交后统一执行。

Parameters

string $table

目标表名(裸 SQL 表名须经 DbSchema::prefix 拼接,勿硬编码 schema)

string $oldAncestors
int $selfId
string $newAncestors
int $levelDelta