joeygroh's blog

Drupal 7 database wrapper awesomeness - how to join multiple tables

With the release of Drupal 7, many major changes have taken place within the architecture that have drastically altered the code writing experience. The revamped database API has been rewritten with oop principals in mind, allowing the abstraction layer to sit on top of more database types than ever before. I've been working on a project that needed to join multiple tables together, so I thought I'd share a little about my experiences. What I've found is the new api makes it insanely easy to build queries!

In past versions, db queries would look something like the following:
db_query("SELECT n.nid, n.title, n.created FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE blah blah blah

So check this out.. Drupal 7 way:
$query = db_select('node', 'n'); //bring in a table $query
->fields('n', array('nid', 'title', 'created')) //get some fields
->condition('n.type', 'news') //add a condition - the third argument defaults to '='
->join('users', 'u', 'u.uid= n.uid'); //and join in a table
$result = $query->execute();

It looks like it's a bit more work, but it's really not. Writing it out that way cuts down on sql mistakes, and make it easy to comment out a line really quick while you are developing (I seem to do that a lot). Next I wanted to add another joined table, so I added another ->join next to the other expecting it to work magically. Nope! I found out that joins can't be chained like the others due to some aliasing issues, so that's where the addJoin method comes in to play.  

$query->addJoin('INNER', 'users_roles', 'ur', 'u.uid = ur.uid');

and how about another…

$query->addJoin('INNER', 'role', 'r', 'ur.rid = r.rid');

and maybe I want to add in the role name also...

$query->addField('r', 'name');

So we end up with something like this (don't use this code directly without proper testing... duh!) :

$query = db_select('node', 'n');
$query
->fields('n', array('nid', 'title', 'created'))
->condition('n.type', 'news')
->join('users', 'u', 'u.uid= n.uid');
$query->addJoin('INNER', 'users_roles', 'ur', 'u.uid = ur.uid');
$query->addJoin('INNER', 'role', 'r', 'ur.rid = r.rid');
$query->addField('r', 'name');
$result = $query->execute();

Then just run a loop on the $result like usual. So as you can see, you can just keep going and going adding things and Drupal does all the work for you! There is SO much more, I wish I could ramble on for pages... but until next time. 
p.s. did I mention how easy it is to do 'transactions' now? ok ok I'll stop :)

I heart Drupal

 It's official, I just bought my ticket to DrupalCon San Francisco 2010!! The first 200 people to sign up were able to get their tickets at the low price of $150. So was I one of those lucky people? Well.. no. Unfortunately for me, I must have been #201 cause I had to pay $195. I'm not complaining though, $195 bucks is well worth it for being able to attend this exciting yearly event - It just would have been cool to say yeah, I was one of the first 200 people, ya know? 

This years conference promises to feature "the most famous technology professionals in the Drupal Community" (http://sf2010.drupal.org/conference) and all the bells and whistles that come with such an event. Big tricked out name tags, case studies, training. interesting new ideas and meeting all the amazing people who are involved. Wow, I wonder if I will be able to meet Dries? If you know nothing about Drupal (shame on you), he's the founder.

In the year 2000 (best if said like the guy from Conan O'Brien), University of Antwerp student Dries Buytaert, put together a site for him and his college buddies to discuss topics such as their work, interesting notes, and where to eat and drink after classes. I find this somewhat interesting as Drupal is now used by a great many businesses and record labels for the primary function of social networking sites. Enough about Dries, I believe some background on Drupal and myself is in order. 

I have been developing sites in Drupal for a few years, and have come to realize how powerful and flexible this open source cms is (I prefer to think of it as a developer platform or content management framework). Being someone who preferred to build content management systems from scratch using php and mysql, I found Drupal to be a solid foundation to build sites on that could be customized quickly to the nth degree. Pretty much anything you might need on your standard website can be found in Drupal core, or in one of the countless contributed modules that are also open source and free to be installed. Don't like how something works? You can change it. Need some help? There are so many people willing to answer questions on forums and in places like IRC rooms that you can take advantage of.

To me, the best thing about Drupal is the community behind it. Dedicated folks putting their time in to contribute, support and grow this massive beast for the good of all mankind. Ok, that is a bit over the top, but you get the point. Some are paid to do it by sponsors, some just do it because the love of being a part of something bigger. Next year is going to be great, and I can't wait for DrupalCon. If I'm gonna be honest, I'm looking forward to the DrupalCon after parties. I will find the best Martini, and report back. I promise. 

Flash Catalyst and Flash Builder 4

As I watched an intro tutorial for the public beta 2 of Adobe Flash Catalyst, I found myself having a hard time saying anything else besides "wow".

Flash designers are used to timelines and creating an experience in a visual environment with a range of tools suited for artists, while Flex programmers build RIAs (Rich Internet applications) using mxml and Actionscript in a mostly code based view environment. (Sure, you can drag and drop components but do programmers really do much visual layout?) Even though Actionscript is used in Flash as well, I will be focusing this example on the Flash designers that use it for design only. Ok where were we... For the most part, developers are not expected to make some cool looking UI, and designers are not expected to program the app. Of course there is always overlap and exceptions, but this seems like the standard way. As a Flash designer who started out using the timeline to create movies, I could definitely see where the line was drawn in the sand when it crossed over to programming. If I couldn't do it with a timeline or a paintbrush tool, it was a job for someone else. As many of you might know, running or working for a small business forces you to become a jack of all trades, hence I eventually picked up programming. As both a designer and a programmer, I can say that making Flex apps look slick is not an easy challenge. Enter Flash Catalyst... 

Flash Catalyst is new to the arsenal of products from Adobe and bridges the gap between designers and programmers. Either a designer or a programmer (or anyone who knows how to use a computer) could create the starting view of an app using this tool quickly and easily. Starting off by importing a layered photoshop or illustrator file, you can end up with a mocked up working app complete with button rollovers, scrolling data lists with dummy data, smooth state transitions, and much more. When the front end is done being built, the file can be imported into Flash Builder 4 where all of the under-workings can be exposed, configured, plugged into live data and transform into a real app. Yeah, it's that cool.

Excitedly waiting for Flash Catalyst to download, I decided that a band widget would be a great first example of what was possible using this new program. I hurriedly started a design in Photoshop - little did I know, this would end up taking the longest time out of all of the steps for building the widget. For those with good Photoshop (or Illustrator) manners, naming layers and folders will be very helpful as components in Flash Catalyst get their names from the layers. I also found out that layer effects don't translate very well, so I ended up going back a few times to flatten the effects into regular layers - the same works with fonts if you don't need to have them dynamically populated later. Upon opening Flash Catalyst, I chose my Photoshop file from the application entry prompt and it began the import. Once the file was loaded, I felt like a kid playing with a new toy Christmas morning. The feeling I got while clicking around was very similar to that of Flash - a visual experience. There is a code view inside Catalyst, but I opted to stick with the default settings as a designer would probably do. I had already seen some video tutorials, so I was somewhat familiar with the HUD, or heads up display, and the other parts of the application like viewing states (very similar to button state viewing in Flash). It took about 15 minutes to put together the button rollovers and the list display, but I imagine it would have only been 2 or 3 if I knew exactly what I was doing in there. So this is supposed to just export to Flash Builder 4 and become an mxml/actionscript project complete with all the necessary things to make it work? Amazingly, that is exactly what happened next. 

Before I installed Flash Builder 4 I had to make sure I could still run Flex 3 as I have ongoing projects, and I'm sure I'll still be using that for some time till Fb 4 is a release candidate. Fortunately, the two apps play nicely and I was on my way importing the fxp file. Things looked the same (thank god) on the outside, but further inspection of the code exposed many new things under the hood. I won't get into all of that here, but it was all necessary to be able to support layers, typefaces, state changes and added namespaces. From here on out it progressed like the other flex apps before it - I didn't go with any MVC architecture, but It looks like this would work very well with most of them. It saved all the components in the components folder, so if you are using Cairngorm and have your components folder buried in com.blahblah.blahblah you'll have to change those declarations.

So how much time was saved with Flash Catalyst? I'm not kidding, I think it would have taken me the greater part of day trying to do the same thing with the way I do things now in flex. In addition, it allowed me to do things I would never have even attempted to do with flex. The UI is mostly graphical and looks like it was made in Flash. The boring greyish looking flex app feeling was nowhere to be found, even though this is in fact a flex app. If I were going to make a larger application, I would definitely incorporate vector art or built in UI components as PNG based files can get rather large if you have too many files. 

That's it for now, take a look at the tutorial links below to see Adobe Flash Catalyst in action. You can see my first app below as well.

Syndicate content