protected static function _generateTree(SimpleXMLElement $xml, $reportBaseId) { static $reportBaseList = array(); $reportBaseClosure = new self(); $descendants = $reportBaseClosure->getClosureTreeById($reportBaseId); $item = null; foreach ($descendants as $row) { if (in_array($row->reportBaseId, $reportBaseList)) { continue; } if ($item === null) { $item = $xml; } $leaf = $item->addChild('row'); $leaf->addAttribute('id', $row->reportBaseId); $leaf->addChild('cell', $row->displayName); $leaf->addChild('cell', $row->systemName); $reportBaseList[] = $row->reportBaseId; if ($reportBaseId != $row->reportBaseId) { // prevents infinite loop self::_generateTree($leaf, $row->reportBaseId); } } }