mineadmin/MineAdmin

[BUG] 后台菜单的level字段出错

Closed this issue · 1 comments

执行命令并粘贴以下结果。

Command: uname -a && php -v && composer info | grep mine && php --ri swoole

# Paste the result here.

bug描述:

当修改了某个菜单的上级菜单后, 如果此菜单有子级菜单,那么子级菜单的 level 字段都没有被正常更新。

如:
图片

当我给 权限 这个菜单添加了父级菜单后, 只有 权限 菜单的level有正常更新,所有子级菜单的 level 都没有更新。

重现步骤:

  1. 添加新的顶级菜单
  2. 权限 菜单的上级设置为刚才添加的顶级菜单
  3. 查看数据表即可发现。

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:

  1. After a menu's parent is updated, retrieve all its child menus.
  2. Update the level field for each child menu based on the new level of its parent.
  3. 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


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.