Ok, my database table for storing my content is a big tree, set up like this id ----- label ----- parentid 1 ---- 'myweb' ---- 0 2 ------ 'links' -------1 3 ----- 'portfolio' ---1 4 ----- 'firstlink' ---- 2
Make sense? Every row has it's own Id, and the ID of its parent, and from this I get a big branching tree structure to hold the data. I'm presented with this problem where I have to take a branch of the tree and make an exact copy of it within the database. The relationships have to be all the same, but all new ids. A copy of the 'myweb' node from the example above looks like this: id ----- label ----- parentid 1 ---- 'myweb' ---- 0 2 ------ 'links' -------1 3 ----- 'portfolio' ---1 4 ----- 'firstlink' ---- 2 5 ---- 'myweb' ---- 0 6 ------ 'links' -------5 7 ----- 'portfolio' ---5 8 ----- 'firstlink' ---- 6 I'm thinking, excellent opportunity to go nuts with CFCs, and write some recursion, right? Wrong. Here's what happened (pseudo-code): 1.Create a function named replicate: 2.Takes two arguments: NodeToDuplicate, and ParentOfNewNode. 3.Selects everything from the table where id = nodeToDuplicate 4.Inserts the data from previous query with parentid = arguments.ParentOfNewNode (this re-creates the node, with a parent that *I've* specified when I invoke the function) 5.Selects the MAX(id) from the table WHERE label = 'thelabelIGotFromTheFirstQuery', sets that to NewNodeId 6.Selects all the ids WHERE parentid = NodeToDuplicate (this gets all children of the current node) call it qGetChildren 7.Loop over qGetChildren Re-invoke the replicate function from within itself for every child of the current node. NodeToDuplicate = qGetChildren.id ParentOfNewNode = NewNodeId (the id of the newly replicated node in step 4) /loop Ok. let that soak in. Should work, right? Right. I thought so too. It fails hard. Gets off-track fast. I go through the logs of what it did (after I reboot the web AND database server) and it's getting down to the first node that has no children... Then looping back to the top of the whole structure, and looping trying to duplicate nodes that have no relationship to the node I'm trying to duplicate, then it gets into a loop where it's running down the left-hand side of the tree from top to bottom over and over until the house-of-cards comes crashing down, and I've killed the server. I'm thinking, 'It's in the logic. My recursion is bad'. Spend about a week on the above 7 points. On a whim, re-encapsulate THE EXACT SAME LOGIC as above into a Custom Tag. Works like a freakin' charm. So, what's the difference? The CFCs are creating multiple threads, and getting out of sync with the database returns? Trying to process the next iteration before it's gotten the return from the database, whereas the custom tag is running more procedurally? Or what? Any thoughts? I'm happy to supply the actual code to anybody who's interested. Willy ----- Willy Ray Web Applications Developer Certified Advanced ColdFusion Developer Westminster College ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4