ok, in that case it's easy and there are a million ways to do it and
you need to decide where in your architecture this should go (and no,
this has nothing to do with Flex and is very common in denormalised
database applications).  

Contrary to my last post I think I would probably do this server-side
right above your database query and serve the xml directly to flex.

Presumably you have a list of the tuples that form the parent child
relationships identical to the first list below which is returned from
your query.

Make yourself an object which can contain children in a list e.g. 
(this example is java, you'll have to translate into the language of
choice - normal text editor coding caveats apply)

class MyObj
{
private List<MyObj> children = new ArrayList<MyObj>();
private Long id = new Long();
--- other properties
--- getters and setters
}

then write a procedure to form the hierarchy from the tuple list...

Map<Long, MyObj> objects = new HashMap<Long, MyObj>();
MyObj parent;
MyObj child;
for (int i = 0; i < my_tuple_list.size(); i++)
{
parent = objects.get(my_tuple_list[i].parentId);
if (parent == null)
{
//parent doesn't exist, so create one
parent = new MyObj();
parent.setId(my_tuple_list[i].parentId);
// set the parent object properties here
objects.put(parent.getId(), parent);
}
child = objects.get(my_tuple_list[i].childId);
if (child == null)
{
//child doesn't exist, so create one
child = new MyObj();
child.setId(my_tuple_list[i].childId);
// set the child object properties here
objects.put(child.getId(), child);
}
// add the child to the parent
parent.getChildren().push(child);
}

now you have your parent child hierarchy embodied in your objects and
you can traverse it recursively and turn it into the XML you need.  

If you are clever about how you create MyObj you'll subclass something
which knows how to serialize itself into xml.  

Send the resulting xml doc to flex directly and it should drop
straight in your tree.

I'll mention scalability caveats with this approach right now as I'm
sure someone reading it will point out that if you get 100,000 tuples
this is probably a very bad way of doing it, and that is true.  But
then presenting them all to your users in a tree is not something
you'll get thanked for either, so you'll need a different UI metaphor
in any case.  It's horses for courses.  If you have under a thousand
nodes in your tree this should be OK.

hth 
Simon

PS if you are wondering how to implement a HashMap in AS3 look at
either the Dictionary object or the Object object...
var o:Object = new Object();
o[key] = another_object; is put
o[key] is get

--- In flexcoders@yahoogroups.com, "ericbichara" <[EMAIL PROTECTED]> wrote:
>
> Hi Simon,
> 
> sorry about the confusion, my question is n# 2 on your list, in fact
> you could say this isn't really a Flex question but i thought id ask
> since maybe someone else had come across it. Ill try to explain it
> more clearly. Using the example i gave previously of my database
> structure:
> 
> Project Id        Parent Id
> 
> 1                             null
> 2                             1
> 3                             1
> 4                             2
> 5                             3
> 6                             5
> 
> From that hierarchy i wish to get something like this:
> 
> <Projects>
>     <Project id="1">
>        <Project id="2">
>           <Project id="4"/>
>        </Project>
>        <Project id="3">
>           <Project id="5">
>              <Project id="6"/>
>           </Project>
>        </Project>
>     </Project>
> </Projects>
> 
> which i would then be able to feed to a Tree Component. What would be
> necessary would be some kind of recursive function to create this XML
> I'm guessing.  I have seen posts of people being able to do this with
> CF: http://tech.groups.yahoo.com/group/flexcoders/message/79643
> 
> but need a way to do this either in PHP or in actionscript.
> 
> Thanks again,
> 
> Eric
> --- In flexcoders@yahoogroups.com, "simonjpalmer" <simonjpalmer@>
> wrote:
> >
> > I've re-read this a few times and I'm not clear what you are asking.
> > Is your question...
> > 1) how do I communicate from a database to a flex app?
> > 2) how do I pivot that parent-child hierarchy into xml?
> > 3) how do I get a tree control to consume xml?
> > 4) some other question?
> >
> > If it is 1 or 3, then there are lots of sample apps and docs around
> > that you can use as a starting point.
> >
> > If it is 2, then I doubt that the tree control will natively interpret
> > a flattened (relational) hierarchy such as the one you depict - I have
> > not seen it, but that doesn't mean it doesn't exist.  Let's hope that
> > some bright spark points out that it does.
> >
> > Otherwise you have a decision on where you would like to perform the
> > processing to pivot those rows into a classic denormalised hierarchy.
> >  You can do it...
> > - in a semantic layer above your data,
> > - in your query (although this is hard and probably not a good idea)
> > - in the application layer processing your database query results
> > - in the business layer
> > - in the model layer in your presentation tier
> > - as an auxilliary rendering step just behind your tree (my personal
> > favourite)
> > - anywhere else it fits
> > I'm sure you'll have a feel for which is the best place for your app.
> >
> > If it is 4, then please re-state your question and I'll have another
> read.
> >
> > Simon
> >
> > --- In flexcoders@yahoogroups.com, "ericbichara" ericbichara@ wrote:
> > >
> > > Hi everyone,
> > >
> > > I need your help! I have a database modeled after the adjacency list
> > > model, for example:
> > >
> > > Id     Parent
> > > 1      null
> > > 2      1
> > > 3      1
> > > 4      2
> > > 5      3
> > > 6      5
> > >
> > > and i need to be able to feed this hierarchy to a Tree component in
> > > flex. I am running a mySQL database and using php through AMFPHP to
> > > pass data between the db and Flex. I have absolutely no idea how i
> > > would go about doing this. I have seen some suggestions on here
> using
> > > CF but unfortunately i have no access to CF. I have also tried
> looking
> > > for examples in Java with no success. I would really appreciate some
> > > help.
> > >
> > > Thanks in advance
> > >
> > > /Eric
> > >
> >
>


Reply via email to