每当我呈现一个菜单项,我从数据库找到的每个菜单项,我用它来检查其子菜单。大量的性能下降呈现菜单子菜单
我的控制器渲染菜单项和递归函数来检测它的子菜单中菜单项映射如下
public function renderAction()
{
$menu = $this -> _request -> getParam('menu');
$itemArray = $this -> getSubItems($menu);
$container = new Zend_Navigation($itemArray);
$this -> view -> navigation() -> setContainer($container);
}
private function getSubItems($menu, $parent = 0) {
$mapperMenuItem = new Apanel_Model_Mapper_MenuItem();
$menuItems = $mapperMenuItem -> getItemsByMenu($menu, $parent);
if(count($menuItems) > 0) {
$itemArray = array();
foreach($menuItems as $item) {
$label = $item -> label;
$uri = $this -> getSubItemUrl($item);
$subItems = $this -> getSubItems($menu, $item -> id);
if(count($subItems)) {
$tArray['pages'] = $subItems;
}
$tArray['label'] = $label;
$tArray['uri'] = $uri;
$itemArray[] = $tArray;
unset($tArray);
}
if(count($itemArray)) {
return $itemArray;
} else {
return null;
}
} else {
return null;
}
}
private function getSubItemUrl($item) {
if(!empty($item -> link)) {
$uri = $item -> link;
} else {
$pageMapper = new Apanel_Model_Mapper_Page();
$details = $pageMapper -> getPageDetails($item -> page_id);
$pageClass = "CMS_Content_Item_".ucwords($details['namespace']);
$page = new $pageClass($item -> page_id);
$title = str_replace(" ", "-", strtolower($details['name']));
$uri = $this -> view -> url(array(
"namespace" => $details['namespace'],
"title" => $title
),'page-view');
}
return $uri;
}
和功能getItemsByMenu
public function getItemsByMenu($menuId, $parent = 0) {
$select = $this -> getDbTable() -> select();
$select -> where("menu_id = ?", $menuId)
-> where("parent = ?", $parent)
-> order("position");
$items = $this -> getDbTable() -> fetchAll($select);
if($items -> count() > 0) {
return $items;
} else {
return null;
}
}
我有4种不同类型的菜单渲染在我的应用程序中,我注意到执行过程中显着的性能下降。我经常得到执行超时,渲染时间和菜单之间的差异大约是35秒,而没有大约22秒。这一切都在localhost。递归中是否有缺陷?我可以采取什么措施来提高代码的性能?
你能张贴getItemsByMenu和getSubItemUrl的代码? –
@TimFountain,我已更新我的问题。 – Starx