fix(department): 更新部门时同步保存基础信息#721
Conversation
📝 Walkthrough概览在 变更
建议的标签
代码审查工作量评估🎯 2 (Simple) | ⏱️ ~10 分钟 诗歌
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
app/Service/Permission/DepartmentService.php
| $this->repository->updateById($id, $data); | ||
| $this->handleEntity($entity, $data); |
There was a problem hiding this comment.
请在 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) 进行关联同步和后续处理。
变更说明
修复部门编辑时基础信息无法更新到数据表的问题。
在原有逻辑中,编辑部门时仅处理了关联数据(如部门成员、负责人等),但缺少对部门基础字段的持久化更新,导致名称、上级部门、
状态等基础信息提交后未正确写入数据库。
本次调整在
DepartmentService::updateById()中补充了基础信息更新逻辑,在处理关联关系前先执行: