Hi....
This is just a fix in case the user pass Doctrine_Records instead of arrays.
<guilhermebla
...@gmail.com> wrote:
> Hi miros,
> I already developed the patch to support this, but only in HYDRATE_ARRAY mode.
> We plan to incorporate it after more tests, so this will be a
> temporary solution to you. At a later stage, we'll incorporate it as
> Doctrine_Tree_NestedSet::toHierarchy(Doctrine_Collection $collection)
> By not, here is my solution... it adds a __children index in the
> converted array record. You can apply another mapping method to remove
> this index if you're probably converting this to XML.
> Here is the probably to-be added method toHierarchy:
> public function toHierarchy($collection)
> {
> // Trees mapped
> $trees = array();
> $l = 0;
> if (count($collection) > 0) {
> // Node Stack. Used to help building the hierarchy
> $stack = array();
> foreach ($collection as $node) {
> $item = $node;
> $item['__children'] = array();
> // Number of stack items
> $l = count($stack);
> // Check if we're dealing with different levels
> while($l > 0 && $stack[$l - 1]['level'] >= $item['level']) {
> array_pop($stack);
> $l--;
> }
> // Stack is empty (we are inspecting the root)
> if ($l == 0) {
> // Assigning the root node
> $i = count($trees);
> $trees[$i] = $item;
> $stack[] = & $trees[$i];
> } else {
> // Add node to parent
> $i = count($stack[$l - 1]['__children']);
> $stack[$l - 1]['__children'][$i] = $item;
> $stack[] = & $stack[$l - 1]['__children'][$i];
> }
> }
> }
> return $trees;
> }
> And here is the method I use to remove the children index to be
> applied in my record->xml converter
> protected function _mapHierarchy($data)
> {
> $children = $data['__children'];
> unset($data['__children'], $data['level']);
> for ($i = 0, $l = count($children); $i < $l; $i++)
> {
> $children[$i] = $this->_mapHierarchy($children[$i]);
> }
> return array_merge($data, $children);
> }
> If you need any assistance, just reply to this message....
> Cheers,
> On Fri, Jul 4, 2008 at 9:39 AM, miros <mmir...@gmail.com> wrote:
>> I have a table that uses nested set.
>> I need to fetch the whole tree and convert it to hierarchical php
>> array.
>> What is the easiest way to do that?
> --
> Guilherme Blanco - Web Developer
> CBC - Certified Bindows Consultant
> Cell Phone: +55 (16) 9166-6902
> MSN: guilhermebla...@hotmail.com
> URL: http://blog.bisna.com
> Rio de Janeiro - RJ/Brazil