Skip to content

fix(department): 更新部门时同步保存基础信息#721

Merged
kanyxmo merged 1 commit intomineadmin:masterfrom
westng:fix/DepartmentService
Apr 9, 2026
Merged

fix(department): 更新部门时同步保存基础信息#721
kanyxmo merged 1 commit intomineadmin:masterfrom
westng:fix/DepartmentService

Conversation

@westng
Copy link
Copy Markdown
Member

@westng westng commented Apr 8, 2026

变更说明

修复部门编辑时基础信息无法更新到数据表的问题。

在原有逻辑中,编辑部门时仅处理了关联数据(如部门成员、负责人等),但缺少对部门基础字段的持久化更新,导致名称、上级部门、
状态等基础信息提交后未正确写入数据库。

本次调整在 DepartmentService::updateById() 中补充了基础信息更新逻辑,在处理关联关系前先执行:

$this->repository->updateById($id, $data);

确保编辑部门时,基础信息与关联数据都能够被正确保存。

## 影响范围

- 部门编辑功能
- 部门基础信息更新流程

## 修复结果

- 编辑部门时,基础信息可以正确更新到数据表
- 原有的部门成员、负责人等关联数据处理逻辑保持不变

## 验证建议

1. 进入部门管理页面,编辑某个部门
2. 修改部门基础信息,例如名称、状态、排序或上级部门
3. 保存后检查页面回显及数据库记录
4. 确认基础信息已正确更新,同时关联数据未受影响

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

## 发布说明

* **Bug Fixes**
* 优化部门信息更新时的数据持久化逻辑,确保关系数据同步前已正确保存,提高数据完整性。

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 8, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 8, 2026

📝 Walkthrough

概览

DepartmentServiceupdateById 方法中添加了显式的仓储更新调用。验证目标实体存在后,在应用关系同步前,现在先调用 DepartmentRepository::updateById() 以确保基础记录被持久化。

变更

内聚体 / 文件 摘要
仓储更新调用
app/Service/Permission/DepartmentService.php
updateById 方法中新增显式的 DepartmentRepository::updateById() 调用,确保在同步 department_usersleader 关系前,基础数据已被持久化到数据库。

建议的标签

bug, size:S

代码审查工作量评估

🎯 2 (Simple) | ⏱️ ~10 分钟

诗歌

🐰 一行调用添其间,
数据先存后同步,
关系纠葛得理顺,
小小修复大作用!✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确概括了主要变更:修复部门更新时基础信息未被保存的问题,与代码变更内容完全对应。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added the bug label Apr 8, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/Service/Permission/DepartmentService.php`:
- Around line 47-48: 在 DepartmentService 的更新逻辑中,不要忽略
DepartmentRepository::updateById($id, $data)
的返回值;先把返回值赋给一个变量并判断是否为真(更新成功),若失败直接返回或抛出错误;如果成功,先对当前实体 $entity 调用
refresh()(确保内存中是最新数据),然后再调用 handleEntity($entity, $data) 进行关联同步和后续处理。
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 79ff48b6-8279-4a80-9a50-5b00ddd2b3aa

📥 Commits

Reviewing files that changed from the base of the PR and between 7948261 and 7805d4b.

📒 Files selected for processing (1)
  • app/Service/Permission/DepartmentService.php

Comment on lines +47 to 48
$this->repository->updateById($id, $data);
$this->handleEntity($entity, $data);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

请在 Line 47 先校验更新结果,并在 Line 48 前刷新实体后再同步关联。

DepartmentRepository::updateById() 返回 bool,当前忽略结果会导致“基础字段更新失败,但 handleEntity 仍执行”的部分成功状态;同时 $entity 仍是旧内存对象。建议先判定更新成功,再 refresh() 后做关系同步。

建议修改
-            $this->repository->updateById($id, $data);
-            $this->handleEntity($entity, $data);
+            $updated = $this->repository->updateById($id, $data);
+            if (! $updated) {
+                throw new \RuntimeException('Failed to update department base fields.');
+            }
+            $entity->refresh();
+            $this->handleEntity($entity, $data);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/Service/Permission/DepartmentService.php` around lines 47 - 48, 在
DepartmentService 的更新逻辑中,不要忽略 DepartmentRepository::updateById($id, $data)
的返回值;先把返回值赋给一个变量并判断是否为真(更新成功),若失败直接返回或抛出错误;如果成功,先对当前实体 $entity 调用
refresh()(确保内存中是最新数据),然后再调用 handleEntity($entity, $data) 进行关联同步和后续处理。

@kanyxmo kanyxmo merged commit a70884c into mineadmin:master Apr 9, 2026
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Permission Service size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants