[BUG] 后台菜单的level字段出错
Closed this issue · 1 comments
To address the issue where child menus' level
fields are not updating after changing a parent menu, you need to implement logic that updates the level
field for all child menus recursively. This ensures the entire menu hierarchy reflects the correct levels after any change in parent-child relationships.
Here's how you can approach this:
- After a menu's parent is updated, retrieve all its child menus.
- Update the
level
field for each child menu based on the new level of its parent. - Apply this process recursively to the children of each child menu.
Implement this logic in the SystemMenuService.php
file or a similar service file responsible for menu updates. Below is a simplified example to guide you:
protected function updateChildMenuLevels(int $parentId, string $newParentLevel): void {
$children = $this->mapper->getChildrenMenus($parentId);
foreach ($children as $child) {
$newLevel = $newParentLevel . ',' . $child['id'];
$this->mapper->update($child['id'], ['level' => $newLevel]);
$this->updateChildMenuLevels($child['id'], $newLevel);
}
}
public function update(mixed $id, array $data): bool {
$currentMenu = $this->mapper->read((int) $id);
$updateResult = $this->mapper->update($id, $data);
if (isset($data['parent_id']) && $data['parent_id'] != $currentMenu['parent_id']) {
$parentMenu = $this->mapper->read((int) $data['parent_id']);
$newParentLevel = $parentMenu['level'] . ',' . $parentMenu['id'];
$this->updateChildMenuLevels((int) $id, $newParentLevel);
}
return $updateResult;
}
This example assumes you have a method getChildrenMenus
to retrieve child menus by parent ID. Adjust the implementation details as necessary to fit your application's structure. The key is to trigger an update to the level
field for all affected menus whenever there's a change in the parent-child relationship, maintaining the integrity of the menu hierarchy.
Details
Sources
- app/System/Service/SystemMenuService.php
- app/System/Database/Seeders/system_menu_seeder.php
- app/System/Model/SystemMenu.php
- src/views/system/logs/apiLog.vue
About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.