sorry,

Last line should be:

c=MyTree(name="c", path="%s.%s" % (a.full_name, "c"), level=a.level+1 ) #and it's c

my fault to use c&p.

Regards.

On 5/17/06, Julio Oña <[EMAIL PROTECTED]> wrote:
Hello,

You could have hierarchical data on a relational database and have a easy way to access it:

http://www.evolt.org/article/Four_ways_to_work_with_hierarchical_data/17/4047/
(I've lost the second link, it has even a book on this kind of structures in RDBMs)

Basically you have to put a column on your SO so it will represent the path of the tree until it.
To get all sons of a node you only have to know its path and select by that file with the path as beginnig
in sqlobject should be something like:

class MyTree(SQLObject):
    name=StringCol()
    path=StringCol() #this will be the place where the tree path should be.
    level=IntCol() #Just to make easier to get next level
    def _get_full_name(self):
        return "%s.%s" % (self.path, self.name)

Suppose we have node and need to look all their childrens:

MyTree.select(AND(MyTree.q.path.startswith (node.full_name+"."),MyTree.level=node.level+1))

Ready!!!
One query gets all, no funny recursive references.

When creating entries you shold do semthing like:
a=MyTree(name="a", path="a", level=0)
b=MyTree(name="b", path="%s.%s" % (a.full_name, "b"), level=a.level+1) #so b is under a
c=MyTree(name="c", path="%s.%s" % (a.full_name, "b"), level=a.level+1 ) #and it's c

This last code is a little ugly but functional.

Regards.




On 5/17/06, Matthew Bevan < [EMAIL PROTECTED]> wrote:

On Wednesday 17 May 2006 10:05, mulicheng wrote:
> I'm just curious.. I've wondered about this problem for a while.  But
> what is the best practice for this type of relationship when selecting
> the child pages for display?  I'm thinking of the case when a page has
> children who also have children and so on.  Is it common practice to
> recursively query the database until all the children are selected?  I
> can't think of any sql oriented way to select all the pages otherwise.

I let SQLObject do whatever it needs to give me child nodes in the easiest way


--
Julio



--
Julio
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to