在FastAdmin ThinkPHP5环境下 关联查询 软删除未生效

问题描述

在后台用户列表管理功能中,用户模型(User)已启用软删除(SoftDelete)并实现邀请人关联查询。但在实际查询时,发现主表(fa_user)的软删除条件未生效,导致已软删除的用户仍会被统计和查询出来。


相关代码片段

1. 用户列表查询方法

php 复制代码
/**
 * 查看
 */
public function index()
{
    $this->relationSearch = true;
    //设置过滤方法
    $this->request->filter(['strip_tags', 'trim']);
    if ($this->request->isAjax()) {
        //如果发送的来源是Selectpage,则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model
            ->with(['withparent'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);
    }
    return $this->view->fetch();
}

2. 用户模型定义

php 复制代码
class User extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'user';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    
    /**
    * 关联邀请人
    **/
    public function withparent()
    {
        return $this->belongsTo('User', 'parent_user_id', 'id', [], 'LEFT')->field('id,username,mobile,nickname')->setEagerlyType(0);
    }
 }

问题分析

  • 实际生成的 SQL 只对关联表(withparent)添加了软删除过滤(withparent.deletetime IS NULL),主表(fa_user)未添加软删除条件(user.deletetime IS NULL)。
  • 结果:主表已软删除的用户仍会被统计和查询出来。
    示例 SQL:
php 复制代码
SELECT
	COUNT(*) AS tp_count
FROM
	`fa_user` `user`
	LEFT JOIN `fa_user` `withparent` ON `user`.`parent_user_id` = `withparent`.`id`
WHERE
	`withparent`.`deletetime` IS NULL
LIMIT 1
  • 如果去掉关联邀请人查询,则主表软删除过滤正常。
  • 该情况未在官方文档中明确说明。

解决方案

手动增加主表软删除过滤条件,即在查询中添加 whereNull('user.deletetime'):

php 复制代码
$list = $this->model
    ->with(['withparent'])
    ->where($where)
    ->whereNull('user.deletetime')
    ->order($sort, $order)
    ->paginate($limit);

最终生成 SQL:

sql 复制代码
SELECT
	COUNT(*) AS tp_count
FROM
	`fa_user` `user`
	LEFT JOIN `fa_user` `withparent` ON `user`.`parent_user_id` = `withparent`.`id`
WHERE (`user`.`deletetime` IS NULL)
	AND `withparent`.`deletetime` IS NULL
LIMIT 1

总结与建议

  • 主表软删除过滤需手动添加:在涉及关联查询时,需注意主表软删除条件不会自动生效,需手动补充。
  • 建议:在类似场景下,务必检查 SQL 生成结果,确保软删除逻辑完整覆盖主表与关联表,避免数据异常。

相关推荐
IpdataCloud1 分钟前
远程办公网络安全中,IP查询工具如何保障数据安全?适用场景与落地指南
tcp/ip·web安全·php
xingpanvip1 小时前
星盘接口开发文档:天象盘接口指南
android·开发语言·python·php·lua
liulian09161 小时前
【Flutter for OpenHarmony第三方库】Flutter for OpenHarmony 离线模式实现:让你的应用无网也能萌萌哒~
开发语言·flutter·华为·php·学习方法·harmonyos
a8a3022 小时前
Laravel 8.x核心特性全面解析
php·laravel
深邃-2 小时前
【Web安全】-Kali,Linux配置(2):Java环境配置,Python环境配置,Conda使用,PIP配置使用,SSH远程登录
java·linux·python·安全·web安全·网络安全·php
2401_873479402 小时前
遭遇DDoS攻击后如何快速分析攻击源?用IP查询+离线库定位异常IP
服务器·开发语言·tcp/ip·php
niucloud-admin11 小时前
PHP V6 单商户常见问题——如何修改访问域名默认跳转端口
php
catchadmin11 小时前
使用 PHP TrueAsync 改造 Laravel 协程异步化的可行路径
开发语言·php·laravel
郑州光合科技余经理12 小时前
同城O2O海外版二次开发实战:从支付网关到配送算法
开发语言·前端·后端·算法·架构·uni-app·php
niucloud-admin13 小时前
PHP V6 单商户常见问题——在线升级版本失败后如何回退版本数据
php