Google Groups Home
Help | Sign in
Fetching Nested Set as hierarchical array
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
miros  
View profile
 More options 4 Jul, 13:39
From: miros <mmir...@gmail.com>
Date: Fri, 4 Jul 2008 05:39:43 -0700 (PDT)
Local: Fri 4 Jul 2008 13:39
Subject: Fetching Nested Set as hierarchical array
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?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guilherme Blanco  
View profile
 More options 4 Jul, 18:32
From: "Guilherme Blanco" <guilhermebla...@gmail.com>
Date: Fri, 4 Jul 2008 14:32:59 -0300
Local: Fri 4 Jul 2008 18:32
Subject: Re: [doctrine-user] Fetching Nested Set as hierarchical array
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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Guilherme Blanco  
View profile
 More options 4 Jul, 18:51
From: "Guilherme Blanco" <guilhermebla...@gmail.com>
Date: Fri, 4 Jul 2008 14:51:12 -0300
Local: Fri 4 Jul 2008 18:51
Subject: Re: [doctrine-user] Fetching Nested Set as hierarchical array
Hi....

change this line:

                             $item = $node;

to this one:

                             $item = ($node instanceof
Doctrine_Record) ? $node->toArray() : $node;

This is just a fix in case the user pass Doctrine_Records instead of arrays.

Cheers,

On Fri, Jul 4, 2008 at 2:32 PM, Guilherme Blanco

--
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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google