nested sets (again) with group_concat()
If you haven't used them before, sub-queries are a very handy addition to your developer's tool kit. Traditionally, they are used to grab parts of data where the grouping in the main SQL query makes it difficult, or for getting similar result sets to outer joins. Going back to our nested set model, if you look at the examples for the adjacency set you can see that when they are getting the path to a node, the result set is one row containing several fields.
So, what do we want to do? We could do it programatically with a loop, and use two queries. I am always incredibly weary about placing queries in loops and I decided against this. All we really need to do is concatenate all the node names together into a string. Makes sense? So as we traverse the path, PORTABLE ELECTRONICS is appended to ELECTRONICS, and then MP3 PLAYERS is appended to the previous string, and finally FLASH is appended to this string.
Unfortunately, this method doesn't work either. Your 'node' and 'parent' elements will concatenate together, but again they will overwrite the previous concatenation. The only way around this is to use an aggregate function that is present in MySQL 4.1 called GROUP_CONCAT(). This will concatenate a field together based upon a GROUP BY clause. If we run the query:
So there you have it. GROUP_CONCAT()… I feel stupid that I never knew about it before. I use it every now and then in queries like this to produce a CSV from a dataset and use WHERE IN() or just return the string and 'explode' the list with PHP. Hopefully my little adventure with the nested set model and this MySQL string function proved useful to you. If you have any further ideas for making the query more efficient, or you can rewrite this without a sub-query, please: leave a comment!
programming ides: annoyances
Some time in 2004 or 2005 whilst I was at university, I was lucky enough to win a copy of Zend Studio 5. Obviously I began to realize what I was missing in my IDE. Before this point I don't think the IDE I was using really crossed my mind. However, the code completion was absolutely excellent (still unrivaled for PHP, I reckon) and the spread of features was awesome. One of my favourites was the built-in support for version control (CVS and Subversion) which became a bit of a killer app for me whilst coding my final year dissertation project.
Now, some of you might have tweaked that I said I switched due to hardware, and Zend Studio (as it is written in Java) is cross-platform. When I started my job I was thrown into the world of Apple Macintosh. After getting used to the basics of the operating system and falling in love with the BSD-esque underbelly, I got my copy of Zend running nicely under OS 9.0 Tiger. After a couple of months of glorious use, it began to get extremely sluggish. Loading it up took 20+ minutes, and each keystroke seemed to lag for about 2 seconds. Obviously, this was completely unusable. Apparently, it's something to do with the cache folder that Zend creates inside the user preferences directory. Despite following those instructions, it didn't remedy the situation.
I moved to OS X and a new (more powerful) iMac hoping that it would remedy the situation. Not really, I found. It still feels much more responsive on my PC in my home office, despite the specifications of the machines not being that distant from each other. In frustration, I went through some other popular IDEs for Mac:-
- Dreamweaver CS3 - Terrible. Honestly, I can't understand why people use this IDE. Crash-happy, slow, irritating text-completion. The site-wide (or project-wide) search is a decent feature, though. I hope more IDEs pick it up.
- Coda - Very enjoyable to use. Obviously an exercise in Cocoaforge for the authors, who have a very good grasp on GUI programming. It is missing certain tools, and some of the features are annoying (and cannot be changed via preferences). The IDE will shine with the advent of modules or plug-ins, if that ever happens.
- TextMate - My current IDE of choice. I switched to this for it's flexibility (code completion via Textmate 'bundles' is a fantastic idea and full-circle, reminds me of UltraEdit32). Thanks to my co-worker for praising it enough for me to take notice(!)
And so, I come to the REAL reason I started writing this blog. The PHPDoc competition for classes in the standard PHPdoc textmate bundle is ass. It is nowhere near as good as Zend Studios… but since that IDE isn't an option for me currently whilst I'm in the office, I've reverted to hacking away at the bundles in order to make it more 'Zend-like'. After reading through and putting into practice the TextMate and phpDoc Comment Blocks article at Killersoft, it became obvious that it is completely possible to do what I am trying to achieve. (Note: if you also follow that article, make sure you get the newlines correct in the bundle- if you copy-paste like I did, it does not work. Get the text-only version they supply and copy-paste that!)
So yep. Zend is great and all, but for the time being I'm flitting between Coda and Textmate. Is there any better alternative for scripting on a Mac? I can't get the Eclipse-based Zend Studio 5.5 by the way, since my license key won't stretch that far, and I refuse to pay for the upgrade!
well... liam made me do it.
I did have a look around this morning at the "blogosphere" (another term I hate. I actually grimaced whilst typing it) and there are so many pages that could be carbon copies of each other. If I have to read another post about some person who just finished an awesome layout filled with AJAX, CSS2.x (compatible with 3.x, naturally!) and XHTML1.1S and the obligatory arse-lick comments from passers by that inevitably come with it, I might take a toaster with me next time I take a bath. I'm all for someone celebrating their achievements, but I'm sick of the same achievement being posted again and again ad infinitum on infinite weblogs. Having said all that, I better watch my step. Don't want to be labeled a hypocrite now, do I?