2013-07-01 56 views
0

在我的模型,我有:ZF2 - 循環的循環

public function fetchForParentId($id) 
{ 
    $resultSet = $this->tableGateway->select(array('acl_menu_parent_id' => $id)); 
    $resultSet->buffer(); 
    $resultSet->next(); 
    return $resultSet; 
} 

和控制器:

$sm = $this->getServiceLocator(); 
    $this->layout()->acl_menu = $sm->get('AdminSettings\Model\AclMenuTable'); 

在我看來,我可以用模型功能:

<?php 
$rows = $this->acl_menu->fetchForParentId(0); 
foreach ($rows as $item): 
    echo $item->acl_menu_name.'<br>'; 
endforeach; 
?> 

它的工作原理,但如果我嘗試運行此:

<?php 
$rows = $this->acl_menu->fetchForParentId(0); 
foreach ($rows as $item): 
    echo $item->acl_menu_name.'<br>'; 
    $subRows = $this->acl_menu->fetchForParentId($item->acl_menu_id); 
    foreach ($subRows as $subItem): 
     echo ' - '.$subItem->acl_menu_name.'<br>'; 
    endforeach; 
endforeach; 
?> 

第二個foreach不起作用,$ rows-> acl_menu_id存在,而在數據庫中有正確的行顯示。

我不知道它爲什麼會發生。

回答

0

它不應該是$item而不是$rows

$subRows = $this->acl_menu->fetchForParentId($item->acl_menu_id); 
+0

對不起我的錯,是完全按照你寫的,而不是作品。 – Nizzre

+0

好的,我重寫了第二個foreach,現在沒關係。一些空白?沒關係。感謝您的時間。 – Nizzre