/** * Finds all datasets of current branch and return in tree order * * @param Tx_WoehrlSeminare_Domain_Model_Category $startCategory * @ignorevalidation $startCategory * @return array The found Category Objects */ public function findCategoryRootline($startCategory = NULL) { $query = $this->createQuery(); $constraints = array(); if ($startCategory !== NULL) { $constraints[] = $query->equals('parents', $startCategory->getUid()); } else { $constraints[] = $query->equals('parent', 0); } if (count($constraints)) { $query->matching($query->logicalAnd($constraints)); } $categories = $query->execute(); $flatCategories = array(); foreach ($categories as $category) { $flatCategories[$category->getUid()] = array('item' => $category, 'parent' => $category->getParent()->current() ? $category->getParent()->current()->getUid() : NULL); } $tree = array(); // if only one categorie exists the foreach-solution below // doesn't work as expected --> take the one and give it back as tree-array() if (count($flatCategories) == 1) { $tree[0] = array_shift($flatCategories); return $tree; } foreach ($flatCategories as $id => &$node) { if ($node['parent'] === NULL) { $tree[$id] =& $node; } else { $flatCategories[$node['parent']]['children'][$id] =& $node; } } return $tree; }