一键导入
list-query-sync
MallBase ThinkPHP 分页 list 和 total 条件同源规则;实现或修改分页列表、搜索筛选和 total 统计时使用。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
MallBase ThinkPHP 分页 list 和 total 条件同源规则;实现或修改分页列表、搜索筛选和 total 统计时使用。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
MallBase 扩展槽边界规范;涉及会员、积分、分销、余额、优惠等权益扩展接入订单、商品编辑或 UniApp 展示时使用。
MallBase schema 真相源与幂等升级 SQL 规则;升级 SQL 仅在用户明确要求已有库升级时使用。
MallBase ThinkPHP 后端规则索引;仅在需要浏览或选择后端规则、且无法确定具体 ThinkPHP skill 时使用。
MallBase 已有库升级 SQL 边界规则;只有用户明确要求考虑已有库升级、测试服/线上升级或 upgrade SQL 时使用。
MallBase ThinkPHP 模型字段访问器与素材回显规则;在 Model 字段读写、访问器、修改器、with 关联、素材 ID 回显或 Service 字段组装优化时使用。
MallBase preview 演示站分支边界规则;涉及 preview 分支、演示站专用功能、main 与 preview 合并、反向合并、演示站部署或公开演示能力修改时使用。
| name | list-query-sync |
| description | MallBase ThinkPHP 分页 list 和 total 条件同源规则;实现或修改分页列表、搜索筛选和 total 统计时使用。 |
适用于 Service 层分页查询方法(例如 getList)。
list 查询用 if/else 手动堆叠条件,total 计数单独重写一遍,两套条件不同步:
// WRONG:list 和 total 是两套独立条件,极易漏加新筛选项
$query = $this->model()->order('id', 'desc');
if (!empty($where['keyword'])) {
$query->whereLike('mobile|email|nickname', "%{$where['keyword']}%");
}
$list = $query->page($page, $limit)->select();
// ← total 单独重写,新增条件忘记加就静默出错
$total = $this->model()
->when(!empty($where['keyword']), ...)
->count();
后果:新增筛选项(如 group_ids、tag_ids)时,只加到 $list 忘记加到 $total,
导致搜索结果 5 条但总数显示 100——分页与前端均异常。
list 与 total 必须复用同一个查询构建方法(如 buildListQuery)。status 条件必须兼容 0 值。public function getList(array $where = [], int $page = 1, int $limit = 15): array
{
$list = $this->buildListQuery($where)->order('id', 'desc')->page($page, $limit)->select();
$total = $this->buildListQuery($where)->count(); // ← 复用,不重写
$list = $list->toArray();
return compact('total', 'list');
}
protected function buildListQuery(array $where)
{
return $this->model()
->when(!empty($where['keyword']), function ($q) use ($where) {
$q->whereLike('mobile|email|nickname', "%{$where['keyword']}%");
})
->when(($where['status'] ?? null) !== null && $where['status'] !== '', function ($q) use ($where) {
$q->where('status', $where['status']);
});
}
status 值为 0(禁用)时 !empty() 会误判为空,必须用:
// 正确:0 值也能通过筛选
($where['status'] ?? null) !== null && $where['status'] !== ''
whereHas 在 Swoole 模式下有已知问题,改用「先查关联 ID → 再 whereIn」:
$groupUserIds = $this->model(UserGroupRelation::class)
->whereIn('group_id', $where['group_ids'])
->column('user_id');
$query->whereIn('id', array_unique($groupUserIds) ?: [0]);
// ↑ 空数组时用 [0],避免 whereIn([]) 返回全量
whereIn 为空时需兜底(如 [0]),避免误查全量。join;若仅做结果展示,优先 with。list 与 total 同步生效。status=0 时筛选结果正确。在以下场景触发此模式:
getList / index)list 和 total(分页结构)total 是否也覆盖backend/app/admin/service/user/UserService.php — 项目中的修复范例(buildListQuery)thinkPHP/list-return-compact — 配套的返回格式规范