From 60f3d1b269fcf4674afe25f9490bd3422a552686 Mon Sep 17 00:00:00 2001 From: Abderrahim CHETIBI Date: Tue, 21 Apr 2020 22:59:35 +0100 Subject: [PATCH 1/2] Fix menu merging array in a loop to reduce execution time --- components/MenuHelper.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/components/MenuHelper.php b/components/MenuHelper.php index 2f2b359c..8f9633d5 100644 --- a/components/MenuHelper.php +++ b/components/MenuHelper.php @@ -113,10 +113,12 @@ public static function getAssignedMenu($userId, $root = null, $callback = null, $assigned = $query->where(['route' => $filter2])->column(); } if (count($filter1)) { + $filtered = []; $query->where('route like :filter'); foreach ($filter1 as $filter) { - $assigned = array_merge($assigned, $query->params([':filter' => $filter])->column()); + $filtered[] = $query->params([':filter' => $filter])->column(); } + $assigned = array_merge($assigned, ...$filtered); } $assigned = static::requiredParent($assigned, $menus); if ($cache !== null) { @@ -143,20 +145,17 @@ public static function getAssignedMenu($userId, $root = null, $callback = null, * Ensure all item menu has parent. * @param array $assigned * @param array $menus - * @return array + * @return void */ - private static function requiredParent($assigned, &$menus) + private static function requiredParent(&$assigned, &$menus) { - $l = count($assigned); - for ($i = 0; $i < $l; $i++) { - $id = $assigned[$i]; - $parent_id = $menus[$id]['parent']; + foreach ($assigned as $item) { + $parent_id = $menus[$item]['parent']; if ($parent_id !== null && !in_array($parent_id, $assigned)) { - $assigned[$l++] = $parent_id; + $assigned[] = $parent_id; + self::requiredParent($assigned, $menus); } } - - return $assigned; } /** From 67786bff6002212276665904a52685acf640c2f7 Mon Sep 17 00:00:00 2001 From: Abderrahim CHETIBI Date: Tue, 21 Apr 2020 23:01:40 +0100 Subject: [PATCH 2/2] Fix n level menu parent display --- components/MenuHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/MenuHelper.php b/components/MenuHelper.php index 8f9633d5..75bc1954 100644 --- a/components/MenuHelper.php +++ b/components/MenuHelper.php @@ -120,7 +120,7 @@ public static function getAssignedMenu($userId, $root = null, $callback = null, } $assigned = array_merge($assigned, ...$filtered); } - $assigned = static::requiredParent($assigned, $menus); + static::requiredParent($assigned, $menus); if ($cache !== null) { $cache->set($key, $assigned, $config->cacheDuration, new TagDependency([ 'tags' => Configs::CACHE_TAG