Developing software in the Real World

Setting up Zend Server 6 on OS X for PHP development

I recently decided to upgrade my Mac's PHP to 5.4. One of the available options is Zend Server 6.1, Free edition. These are my notes how to set it up so that it works the way I develop. Installation Mount the disk image and follow the installation wizard. On OS X, Zend Server installs PHP, Apache and MySQL inside /usr/local/zend. The Zend Server admin interface is at at http://localhost:10081. On first run you have to… continue reading.

Password less command line scripts with MySQL 5.6

I have a number of command line scripts that copy MySQL databases down from staging servers and store them locally. These scripts set the password on the command line using the -p option. With MySQL 5.6, a new warning is displayed every time my scripts run:

This is annoying! The way to solve this is to use the new command line tool mysql_config_editor to set up a "login path" which the mysql command line… continue reading.

Configuring a ZF2 view helper before rendering

The currencyFormat view helper is very easy to use:

When I was reading the documentation for the currencyFormat view helper, I discovered that you could configure the currency code and locale once rather than in every call:

This is obviously useful, but even more useful would be if we could set it once by default and then override if we need to in a specific call. The easiest way to do this is… continue reading.

Using PHP's NumberFormatter to format currencies

I've been using number_format() for a very long time, but recently discovered that within the intl extension there's a NumberFormatter class available too. This is quite a clever class as it is Locale aware and handles formatting currency, including the correct symbol. You can check if you have the intl extension installed using php -m | grep intl and if you don't then you can install it with apt-get install php5-intl or yum install php-intl… continue reading.

Returning a ZF2 HydratingResultSet when starting with raw SQL

If you're using Zend Framework 2's Zend\Db and want to write raw SQL that returns a HydratingResultSet, then you can do this:

When you iterate over the HydratingResultSet, you get a populated instance of MyEntity. The $hydrator instance is the object that knows how to populate your entity object. In my case, I've used an ArraySerializable hydrator, but there's other options available, or you can write your own. As an aside, if you don't… continue reading.

Displaying the generated SQL from a ZendDbSql object

If you use ZendDbSql to generate your SQL, then it's useful to find out what the generated SQL looks like. Consider code like this:

To find out what the generated SQL will look like, you can use the $select's getSqlString() method:

For me, this results in lots of warnings along the lines of: This is less than helpful, so to avoid the warnings, you need to supply the correct platform information to the… continue reading.

IN and Zend\Db\Sql's where()

This is a short note to myself. Zend\Db\Sql objects allow you to do this:

which generates the (My)SQL:

If you want the SQL generated to use the IN operator, then just pass in an array:

which generates:

This obviously also works for ZendDbSql's update() as well as select().

Caching your ZF2 merged configuration

Zend Framework 2's ModuleManager has the ability to cache the merged configuration information for your application. This is very useful as it allows you to separate out your configuration within the config/autoload directory into logical files without worrying about the performance implications of lots of files. Enabling caching is simply a case of setting these configuration keys in config/application.config.php within the module_listener_options section:

This then creates the cache files data/cache/module-classmap-cache.php and data/cache/module-config-cache.php and you're… continue reading.

Contracts and NDAs for Nineteen Feet

Now that I have my own business, I've become an avid listener to the Unfinished Business podcast presented by Anna Debenham and Andy Clarke. From the first episode, they have emphasised the importance of having a contract and, in a recent episode, why an NDA is a good idea. I don't know about you, but when I hear the words "contract" and "NDA", the word "incomprehensible" comes to mind and I was not enthused at… continue reading.

Injecting configuration into a ZF2 controller

One thing you may find yourself needing to do is access configuration information in a controller or service class. The easiest way to do this is to use the ServiceManger's initialiser feature. This allows you to write one piece of injection code that can be applied to multiple objects. It's easier to show this in action! Let's assume that we have this configuration file: config/autoload/global.php:

That is, we have a key called 'application' that… continue reading.