20
May '13

I’ve had cause to learn to use Laravel 4 for a new project I’m working on and I have to say, I’m very impressed. There are many great tutorials out there so I won’t go into details about the syntax, but the philosophy behind it is really worth talking about.

It’s built around the idea that most frameworks include a whole host of features that, while all useful in their own ways, won’t all be needed for every site you build. So the basic Laravel is very lightweight out of the box and uses Composer to manage and install any dependencies needed for your particular project.

Laravel comes with Artisan, a command line utility that provides a great set of functionality and shortcuts, such as generating controllers and models, creating and running migrations and regenerating the autoload scripts. If you’re familiar with Ruby on Rails you’ll see a lot of similarity with the Rails command line tool.

Laravel’s ORM is called Eloquent is and is the most powerful I’ve used for PHP. The support for migrations and seeding is well thought out and easy to use, the relationship management (has many, belongs to etc) is excellent, and it will even handle pivot tables for you without the need for them to have their own models. While it doesn’t have support for building models from an existing database, there are many tools out there that will do the job for you.

If you want more low level control of your database, Laravel’s query builder, whilst reminiscent of Doctrine, has very clean syntax and allows you to construct complex queries in a readable and logical format.

I could go over many many other aspects of Laravel, such as the incredibly clean Blade templating, the routing engine that is so advanced that you can do away with controllers for many applications and the built in Swift Mailer integration but the best recommendation I can make is for you to have a go yourself. There have been a great many ‘next big things’ in PHP that have turned out to have little staying power, but I truly believe that Laravel will have a profound impact on the PHP community, and show us all a new way of approaching our projects.

02
Feb '13

I do a lot of data warehousing work these days which often means scripting a series of heavy queries to process data into a more usable format. I built a fairly simple execution framework to make it easier to cron the scripts to run at night when the load on the servers will be lightest but I found that often I would arrive at work the next morning to find that the scripts had failed to complete. After a little testing and logging it appeared that I was getting ‘MySQL has Gone Away’ errors from the Zend adapter I was using for my connection. Bugger.

Actually it turns out that fixing Zend to reconnect automatically when it loses its MySQL connection is a pretty quick hack with some careful use of the ping function.

Zend has a _connect() function in Zend/Db/Adapter/Mysqli.php that begins with a check to see if it has a connection object already created:

if ($this->_connection) {
    return;
}

Unfortunately if ‘MySQL has Gone Away’ this check is too basic to realise. All you need to do to improve this situation is to add a ping to this check:

if ($this->_connection && $this->_connection->ping()) {
    return;
}

And you’re good to go! Zend will automatically reconnect whenever it loses its connection. Winner.