Introduction to Zend_Db

I recently worked on a project which was based on Zend Framework – I haven’t worked with it before and I was temporarily confused by the existing implementation of some of the database-level stuff. After much reading and untangling of code, I’m now pretty clear how this should look, so here’s my overview. I’m not going to go into setting up a whole application, but this is a quick primer on how data models go together.

Modelling Tables

Zend_Db_Table is a class that represents a table. You instantiate one of these and call query functions against it. The actual code for my classes is really minimal, here’s an example:

class UserTable extends Zend_Db_Table
{
    protected $_name = 'users';
    protected $_rowClass = 'User';
}

The two properties that I’m setting here are all I use for basic tables. $_name tells Zend Framework which database table to use. $_rowClass tells it which class it should instantiate for the results from your select query. By default you’ll get Zend_Db_Table_Row objects but you can have it use any object which extends this class instead.

Getting Results

To get results from a table, you instantiate the relevant table class, and then fetch some results. You can restrict the results you get but Zend_Db_Select is a whole other kettle of fish that perhaps I’ll post about some other day. So a controller action might look something like this:

function viewAction()
{
    $users = new UserTable();
    $user_list = $users->fetchAll();
}

The $user_list variable will then hold an instance of Zend_Db_Rowset.

Working with Rowsets

The rowsets are iterators, you can loop over them and they will return objects which represent the rows in the resultset of the query. By default these are objects of type Zend_Db_Table_Row but if you set the table object’s rowClass, either in the object declaration or by calling setRowClass() before you fetch the results, then you can have any class which extends Zend_Db_Table_Row used. Its pretty common to pass the rowset straight to the view and iterate over it there – you can put any functionality you need for this object into the class you use. Let’s look at my user class for this example.

The Row Class

    class User extends Zend_Db_Table_Row
    {
        public function getName()
        {
            return $this->first_name . ' '.$this->last_name;
        }

        public function getProfileUrl()
        {
            return '/users/profile/id/' . $this->id;
        }
    }

These are overly simple examples, but giving this kind of functionality to the user model allows it to be re-used any place you need it. I’ve also found it useful to have an intermediate table row class that contains functionality that will be used by data from more than one table – for example for formatting dates in a consistent manner.

Zend Framework and Models

This is a pretty simple overview but it took me a while to get to this point – the framework does have documentation but its quite case-specific and doesn’t really show how it ties together. Now I understand these classes and their relationships, my development project is going a lot more smoothly.

Opera’s Address Bar AutoCompletion

I recently upgraded the copy of Opera on one of my machines, only to find a few things about it which had changed from previous versions were driving me mad!! Immediately noticeable was that the address bar autocompletion seemed to have gone completely nuts. It was autocompleting all sorts of addresses that bore no resemblance to what I was typing, and it was giving me deep-link choices first, so I couldn’t autocomplete to just the domain I wanted, for example.

It turns out that this new behaviour is, in fact, a feature – Opera remembers the content of all the pages you’ve been to as well as just their URLs, and then it tries to give you the most relevant matches in your address bar. Well that doesn’t work for me. Address bars are for typing addresses and search engines are for searching content in my world, maybe I’ll find this useful one day but that day isn’t right now. The good news is, its easy to turn off.

Just go to Preferences -> Advanced -> History and then uncheck the box “Remember Content on Visited Pages”. Now when I start typing, I just get the URLs that look like my actual words, and with the shortest matches first. I’m safe to upgrade the other machines now!

Man-fit T-shirt to Girl Top

I am sick beyond description of conference shirts, they are big and they are sack-like. I have lots of them, I’m expected to wear them a lot, and they are not particularly confidence-boosting. Dressing up for the crowd at technical events is not the way forward, but I am a tiny bit tired of shapeless – so I decided to learn how to refashion this pile of shirts that I now have, into something I might want to wear.

I began with this rather excellent tutorial, and an oversized shirt (my sister brought this back from scout camp last year).

tshirt before refashioning

I also took a favourite t-shirt that fits me nicely (not too tight, the finished article is still going to be a t-shirt) and used this as a template. Clothes that fit are pretty rare as I’m above average height, but I have a few favourites. In a nutshell, you draw around the main body of your template shirt (I used kiddy chalk, because it was handy), then make sleeves but by lining your template shirt up against the cuffs of your victim shirt.

outlines on shirt

I tacked (by hand) up the marked side seams, and tried the shirt on (inside out) to make sure it fit and I could get into it and so on (its easy to overestimate how stretchy something is and not be able to actually get into the finished article!). It was fine so I cut out along the chalk lines.

The only really tricky part came next … fitting the sleeves. In fact I had to phone home for some help (thanks mum!) and it turns out what you do is: Put the shirt down inside out and the sleeve the right way out, but post the sleeve in through the armhole with the hand-end of the sleeve inward-most – like a top with the arm inside out. Then the pins go inside this inside-out sleeve, at right angles to how you are going to sew, and with the points pointing out at you. Then you sew round the armhole and just sew over the pins with the machine. I hope that makes sense – I’m hoping it’ll remind me for next time anyway (/me points and laughs at future self reading these instructions)

Sew round the sleeves and down the sides, and take out the tacking. In theory you should also overstitch the hems but … I didn’t quite manage to wait that long before trying on my new shirt.

shirt after refashioning shirt after refashioning

Screen for Linux

I use a lot of apps from the command-line, partly because command-line stuff is the same on every system, and partly because I struggle to use a mouse for RSI reasons. Unsurprisingly, the best keyboard-navigable apps are ones that only use the keyboard in the first place! One app that I can’t live without is screen which allows me to have one ssh connection with multiple windows in it. What’s even better is that I can detach and reattach it – so if I’ve got a complicated set of screens open on a server, or even if I’m just using IRC over a dodgy connection, I can run it in screen and go back and re-attach when I need to.

I use a custom .screenrc file, which adds numbered labels so show which screens I have open, so I see something like this at the bottom of my screen (click on it to make it normal size, the thumbnail is somehow tiny):

My .screenrc file looks like this:

termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
termcapinfo xterm ti@:te@
shelltitle '$ |bash'

termcapinfo rxvt 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
termcapinfo rxvt ti@:te@

hardstatus on
hardstatus alwayslastline
hardstatus string "%{wk}%H%{Bk}|%{Mk}%?%-Lw%?%{km}[%n*%f %t]%?(%u)%?%{mk}%?%+Lw%? %=%{Bk}"

Don’t ask me what any of it does, I got it from someone else and just copy it around machines all the time! The tabs create themselves when you create a new screen (ctrl + a, c) and then you can edit the labels with ctrl + a, A. What else can you do with .screenrc? I must confess I’ve never really looked

How to Submit a Conference Talk

Speaking at conferences is a great way to share ideas and meet people – but actually getting the opportunity to do is a little more tricky and usually involves proposing a talk. In the last year I’ve attended IPC in Germany and PHP London, spoken at DPC in Amsterdam, submitted talks to and attended ZendCon, and helped select the sessions for phpnw – so I’ve seen it from all angles.

The first thing to say about submitting talks, is that there are no pre-requisites. You don’t need to be published, well-known, or have letters after your name (in the PHP community, the latter is probably more hindrance than help). If you want to go to a conference, and there is a topic you’d like to share some thoughts on, then write them down and submit! A lot of conferences have a Call for Papers – usually this will be an online form where you put in your personal details and the details of the talk you’d like to give. If it sounds simple, that’s because it really is …

Proposing your talk

It can be tricky to know what to write in the boxes and how to sell your talk to the conference organisers. The call for papers should give information about the themes of the conference, the expected audience, and the kind of content they are looking for – so pay attention to this. Usually you’ll be expected to submit an “abstract”, this is a description of your talk that will be put on the schedule if you are accepted. A good way to get started with these is to read the abstracts from current conferences – these are the ones that got through the selection process and will give you a good idea of what you should say here. Its usual to also be asked to supply a biography, either when you submit your talk or when the talk gets announced as part of the conference schedule.

If there is room for additional information, then give it – and give the organisers as many opportunities as possible to feel like you would be a positive and safe addition to their event. I’ve seen a few variations on these but for the phpnw call for papers, we added a box which we didn’t publish the contents of and where speakers could tell us why we should have them and/or their talk. This was illuminating, responses varied from “because this topic is so cool!” to “not sure really, thought it might be interesting though” and the unforgettable “meow” (that last one was from an entry that didn’t get accepted – it was hard to tell if the speaker was taking the whole thing seriously or not).

My advice is to start planning your submission in plenty of time – take a look at the information that you will need to supply and make sure you have it all (and do write in the optional boxes). Its also a really good idea to bounce your idea off some other people, who can help proofread and point out any obvious problems with your submission – for example the time I tried to submit a talk to a PHP conference without the word “PHP” anywhere in my proposal …

Getting your Talk Accepted

I have yet to successfully submit a talk via a Call for Papers and be accepted to speak at a conference – so I have no idea how to get talks accepted. If anyone else can add advice on this topic, that would be great :)

PHPNW: One Month Countdown

In a month’s time I’ll be in Manchester, ready for the PHP North West conference. The conference is a one-day event (Saturday, 22nd November), although the social side of things will kick off the night before. Tickets are 50 GBP for the early bird, 35 GBP for students and concessions – so register now.

There are some amazing speakers, I hate picking out names, so go and look at the schedule and pick your own favourites to shortlist! As well as traditional hour-long conference slots, we’ve got a selection of shorter talks, plus a panel discussion at the end of the day … right before we party some more :)

Attendees get a year’s subscription to php|architect magazine with their tickets, and there will be sponsors and other exhibitors there – including some interesting user and voluntary groups so plenty to see and plenty of people to talk to. There are also some very nice giveaways so look out for those if you are there.

All in all, its a pretty exciting event, there hasn’t been anything like it outside of London that I know of for a while – and with the London conference still months away, this is a great chance to get to meet a few people and also pick up some new technical ideas in the meantime. If you’re coming, let me know so I can say “hi” at the event itself – looking forward to meeting you :)

Crochet Tutorial: Next Steps

If you’ve been following the previous entries in this series, you’ll have seen how to start to crochet, and if you’ve followed the instructions you should be able to add another couple of rounds onto your project and end up with something that looks like this:

granny square

There are a number of things you can do with these little squares. They’re a very traditional form of crochet (and a really good way of using up odds and ends), you can see the kind of thing I mean if you search for “granny square” on flickr. When I was first learning to crochet I made myself a coding blanket that I still love!

granny squares blanket

Crochet doesn’t have to be square and it doesn’t have to be traditional – I’ve seen everything from the subversive (crochet covers on parking meters) to the cute (amigurumi). I’m currently working on (currently in the sense that I’ve begun and I haven’t finished yet, rather than it being truly ongoing) a set of hexagonal string coasters. The idea is that they will tesselate and form either a big placemat to put hot pots on or several smaller cup-sized coasters. They’re not radical, but they’re not really your traditional granny square either!

granny hexagon string coasters

I’m sure there are many more uses of crochet in general and granny squares in particular – answers in the comments please :)

rsnapshot flag for usb drive

rsnapshot is a great tool that I use for all my backup stuff, its really easy and seems pretty robust (or rather, I haven’t broken it yet!). When I set it up most recently, I discovered that it has a very useful flag, no_create_root. The problem I often have is that since I back up to removable media, if the disk isn’t mounted, rsnapshot will back up to the local drive instead, in the mount point – and then I’ll promptly run out of space. Stopping rsnapshot from creating directories means no writing rubbish into your mount point, and this setting is designed specifically for this issue with removable media.

This setting does have a gotcha, however. It does the check for whether the directory exists before it calls the script named in cmd_preexec – so if you were hoping to mount your drive in the pre-exec script, you can’t! I was very confused why my rsnapshot configuration didn’t work to start with. My workaround is to run a separate mount script before calling rsnapshot in a cron job, not ideal but it does work for me.

PHPNW Tickets On Sale

Tickets are now on sale for PHPNW – the PHP Conference in Manchester, UK, on November 22nd. This is a conference aimed at bringing together and promoting the amazing wealth of local talent and activity in PHP within the North West and wider area. The schedule is online already and tickets are priced at a very reasonable and credit-crunch-friendly £50 (with discounts for students and OAPs) – and all that isn’t enough to persuade you, remember I’ll be there on the day too :)

3-minute Crafty Earring Tidy

Recently I was shopping for an embriodery hoop and I saw that you can buy ones which are ready-made picture frames, you literally put the fabric in, embrioder, then trim off the outside and tidy up the back. I decided that this would make a great basis for an earring tidy – I try to keep my earrings linked together in pairs, but it depends what kind of butterfly they have and whether I remember! Some days its a real challenge to find a matching pair at all, and looking for a particular pair of earrings is usually a waste of time.

Enter the earring tidy, my 3-minute craft project! Take some fabric ( mine is linen, so its easy to put the earrings through ), put into the hoop, trim. Now add earrings!

earring tidy

It would be cool to categorise earrings and embroider in some outlines and labels, but I didn’t bother. This now hangs by my mirror on a piece of string so I can pick it up and get the earrings easily.