RE: [Zope] Tree tag and SQL methods?
It worked! Thanks. Btw, this looks like HOWTO material to me. As for the infinite recursion, I'd already included such a guard in my own SQL code, but I deleted in the quoted code to make it clearer. ;-) -- Alexander Staubo http://www.mop.no/~alex/ "`This must be Thursday,' said Arthur to himself, sinking low over his beer, `I never could get the hang of Thursdays.'" --Douglas Adams, _The Hitchhiker's Guide to the Galaxy_
-----Original Message----- From: Phillip J. Eby [mailto:pje@telecommunity.com] Sent: 28. juni 1999 01:11 To: Alexander Staubo; Zope Mailing List (E-mail) Subject: RE: [Zope] Tree tag and SQL methods?
Aha. I've got it now. Change the tree tag to read:
<!--#tree id=publication branches_expr="Data.pb_sql(pb = publication)"-->
I reproduced your setup with a query of my own (totally different format), and got the same problem as you. That is, the first level showed but the plus signs wouldn't expand anything. The problem isn't with the queries; it's that the tree tag needs a persistent ID value to keep track of what branches are open/closed. By default it uses the object's persistent ID, if it has one, otherwise it uses the Python 'id()' value. However, if the object is the result of an SQL query, there's no persistent ID, and the Python id() will change from hit to hit. So when using query results, it's necessary to do an 'id=primary_key_field_name' in your tree tag to ensure it'll actually work as a tree. I was doing this in all my current production code, but I overlooked it when describing to you what I did.
By the way, as you have your query constructed, it will cause the tree to infinitely recurse once you hit the second level. It would be good to restructure it a bit, as follows:
<!--#if "pb == ''"--> select distinct publication, count(publication) as pub_cnt from articles where publication != '' group by publication order by publication <!--#else--> select title, 'nosuchpublication' as publication from articles where publication = <!--#sqlvar pb type=string--> <!--#/if-->
Without this change, the tree tag will not realize the articles are leaves, because when it calls the query to check on them, it will return all the publications again.
At 12:39 AM 6/28/99 +0200, Alexander Staubo wrote:
Here you go:
<!--#with "_.namespace(publication = '')"--> <!--#tree branches_expr="Data.pb_sql(pb = publication)"-->
<!--#var publication--> <!--#/tree--> <!--#/with-->
I've been following the dialogue between Alexander and Phillip, and today things started to click so I thought I'd have a go... :) My database consists of two tables, familytree and element. familytree holds parentage and element holds the data. My SQL query (get_parent_child_topics(theparent:int, flag:int)) is; <!--#if "flag==0"--> select thetag, level, header, content, child, child as flag from familytree f, element e where tag_id = <!--#var theparent--> and f.child = <!--#var theparent --> and thetag='topic' order by child <!--#else--> select thetag, level, header, content, child, child as flag from familytree f, element e where f.parent= <!--#var theparent--> and child = tag_id and thetag='topic' order by child <!--#/if--> and the DTML that calls it is ; <!--#with "_.namespace(child=135,flag=0)"--> <!--#tree id=level nowrap=1 single=1 branches_expr="get_parent_child_topics(theparent=child,flag=flag)"--> <a href="show_headers?child=<!--#var child-->"><!--#var header--></a> <!--#/tree--> <!--#/with--> This works beautifully ... almost. Initially, I get the top level line (Theme A:The Cell). Clicking on the 'open box' I get the next level; Cells and tissues, structure and function Molecular Basis Of Medicine Gene Expression Immunology Course Medical Microbiology Human Genetics Molecular Medicine General Pathology etc... all with their own 'open boxes'. Problems start if I click on one of these open boxes (say Medical Microbiology). That level expands out, as well as all the others (Gene Expression etc.). I have a gut feeling that I'm close to getting this sorted out, but I can't see where I might be going wrong. Any ideas would of course be appreciated... ta, tone. ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
At 04:40 PM 6/28/99 +0100, Tony McDonald wrote:
I've been following the dialogue between Alexander and Phillip, and today things started to click so I thought I'd have a go... :)
<!--#with "_.namespace(child=135,flag=0)"--> <!--#tree id=level nowrap=1 single=1 branches_expr="get_parent_child_topics(theparent=child,flag=flag)"-->
Before anything else, I'd suggest you pick another field than level as your id. The problem is that everything on the same level has the same id, so when you click it open, everything on that level opens. You need something that will be different for every possible line in your tree. If all else fails, you could use a pluggable brain to generate an id value from a hash of some fields, if you don't have a nice short primary key (like an integer or other ID code). That's assuming there isn't an "id_expr" option that'll do the trick. It's been a while since I looked at the tree tag source.
At 10:59 am -0500 28/6/99, Phillip J. Eby wrote:
At 04:40 PM 6/28/99 +0100, Tony McDonald wrote:
I've been following the dialogue between Alexander and Phillip, and today things started to click so I thought I'd have a go... :)
<!--#with "_.namespace(child=135,flag=0)"--> <!--#tree id=level nowrap=1 single=1 branches_expr="get_parent_child_topics(theparent=child,flag=flag)"-->
Before anything else, I'd suggest you pick another field than level as your id. The problem is that everything on the same level has the same id, so when you click it open, everything on that level opens. You need something that will be different for every possible line in your tree.
That's it! - I set id to 'child' (gauranteed to be unique in my db) and bingo! This is so cool! many thanks Phillip. Tone (tres pleased now!). ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
participants (3)
-
Alexander Staubo -
Phillip J. Eby -
Tony McDonald