2010
05.25

I’ve just finished a few weeks’ work writing an ORM library for PHP5. Initially I was going to use one of the existing ORM libraries which are already available, but after researching I discovered that most were either too large and clunky for everyday use, didn’t have enough features, or didn’t conform to my own personal coding preferences. So I decided to code my own from scratch. The result: an ORM library that fits all my current requirements and doesn’t take forever to set up.

To start with I wanted an ORM library which didn’t require much in the way of configuration. I’ve therefore made sure that mine follows the ‘convention over configuration‘ principle, meaning I can get the library set up quickly and effectively, without having to specify every database table, relationship and column that my database models consist of. My conventions currently consist of the following:

  • Ensuring all database table names relate to the ORM class we’re using for the model, for example, a Customer model would require a corresponding ‘customers’ database table, where as an Account model would require an ‘accounts’ database table for storing its’ data. The library automatically knows which database table it needs to use through an inflection library, but of course this can also be over-ridden on a per-class basis by setting a static $table property;
  • Ensuring that all tables related to ORM classes have an auto-incrementing ‘id’ column for their primary key. I may allow this to be overridable in the future, but for now this should suffice;
  • Ensuring that all foreign keys follow their related table’s name prepended with ‘_id’, so an ‘accounts’ database table may have a ‘customer_id’ column, which would link it (many-to-one relationship) to the ‘customers’ table. Once again the inflection class has been used here to work this out automatically.

The whole reason I favour using ORM over writing multiple SQL statements is that for the most common operations (inserting, updating, deleting, selecting, etc) I can put OOP practices to good use to do the hard work for me. I’ve provided some examples below of just how simple it is to use the library currently:

$customer = orm(Customer)->fetch(100); // Fetch customer 100 from the database
$customer->first_name(“John”); // Change the customer’s first name
echo $customer->surname(); // Print out the customer’s surname to screen
orm()->save($customer); // Save the changes back to the database

$account = orm(Account)->fetch_by_domain(“example.com”); // Fetch an account based on a different column

orm(User)->fetch_by_email_address(“joe@bloggs.com”)->delete(); // Delete a user’s record from the database, based on their email address

Of course, for anything more complex (multiple joins across more than one or two tables, unions, sub-selects, etc) I will still need to write the SQL and execute this manually, but this still suits my needs as it is beneficial to be able to see the exact SQL in these cases, rather than it being abstracted by a library.

My ORM library will now be put to good use on the new Hartserver backend, and I may even consider releasing it publically in the future as and when I add even more features to it.

2010
05.20

The other month I was reading about a MobileMe ’scraper’ class written in PHP called Sosumi. This got my mind rolling…

Since I already have a MobileMe account which provides the ‘Find My iPhone’ feature to check on the (near enough) real-time location of your iPhone, I wanted to make use of this. Now I thought, wouldn’t it be great if you could be checked into Foursquare venues automatically without having to open the app on your iPhone and check in manually. Well, yes, this would be brilliant! However, there’s one major technical limitation. Apple have disallowed any form of background processes for third-party applications. Due to this, it is impossible for the Foursquare app to know your location all of the time without being constantly open all of the time. This would be a major pain as you can rightly imagine.

This is where Sosumi comes into its own. Sosumi is run on a webserver, the same way as any other PHP code. This simply talks to the MobileMe ‘Find My iPhone’ process, which eventually returns a simple pair of latitude and longitude co-ordinates. I went about setting up a CRON script to call the Sosumi script every 5 minutes, which would log into MobileMe and check the location of my iPhone. This worked well. A little too well to be honest! The next step was to talk to the Foursquare API.

I ended up using CURL in PHP to request all venues on Foursquare closest to the co-ordinates Sosumi returned from MobileMe. Here I stumbled upon a problem however. It seemed that the iPhone had a tendency to return duff co-ordinates in some cases, I’m assuming when the phone has been in sleep mode for a good while, and doesn’t update a truly accurate location to MobileMe. Either way, this and the fact my CRON script was running every 5 minutes was causing a lot of random check-ins to all sorts of nearby venues!

I’ve overcome this issue now by reducing the period the CRON job is executed, as well as ensuring that no double check-in occurs if you’re already in the same location. Sosumi is now scraping MobileMe for co-ordinates every 15 minutes, which seems to work well. I’ve also come to the conclusion that if you’re not staying in the same location for more than 15 minutes then it’s pointless checking in at all.

So there you have it! I now benefit from auto check-ins wherever I roam! I’m considering creating a public service now for this so that anyone else who has a MobileMe account can simply enter their login details and that’s it – automated check-ins for all MobileMe iPhone users!

2009
10.23

Welcome to my new blog!

Hey!

I was becoming increasingly annoyed at having a blank (and rather terrible!) holding page on my personal site for far too long! I just don’t seem to have the time to design and build an entire personal site at the minute, so I gathered the next best thing would be to get a blog up and running on here. Well, here it is!

I’ll probably be blogging mainly about coding (PHP more than anything), with a few code examples thrown in there. Also, I’ll share some info on any new and exciting projects I’m working on. Apart from that, my other general ramblings on life will no doubt be found on my Twitter feed.

That’s all for now. Will blog again soon. :)

Harty x