Pragmatism 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: Warning: Using a password on the command line interface can be insecure. This is annoying! The way to solve this is to use the new command line tool mysql_config_editor… continue reading.

Configuring a ZF2 view helper before rendering

The currencyFormat view helper is very easy to use: echo $this->currencyFormat($value, 'GBP', 'en_GB'); 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: // Within your view script $this->plugin("currencyformat")->setCurrencyCode("GBP")->setLocale("en_GB"); 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… 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: use Zend\Db\Adapter\AdapterAwareInterface; use Zend\Db\Adapter\Adapter; use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Db\ResultSet\HydratingResultSet; use Zend\Db\Adapter\DriverResultInterface; use MyEntity\MyEntity; // some sort of entity object class MyMapper implements AdapterAwareInterface { protected $dbAdapter; public function fetchRowsWithValue($fieldValue) { $sql = "SELECT * FROM my_table_name WHERE my_field_name = ? "; $params = array( $fieldValue, ); return $this->fetch($sql, $params); } protected function fetch($sql, $params… 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: public function fetchAllWithTitleSince($title, $since) { $sql = new Sql($this->dbAdapter); $select = $sql->select(); $select->from($this->tableName); $select->columns(array('id', 'title', 'url', 'date_updated')); $select->where->like('title', "%$title%"); $select->where->greaterThanOrEqualTo('date_created', date('Y-m-d', strtotime($since))); $statement = $this->dbAdapter->createStatement(); $select->prepareStatement($this->dbAdapter, $statement); return $statement->execute(); } To find out what the generated SQL will look like, you can use the $select's getSqlString() method: $select->getSqlString(); For me, this… 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: $id = 2; $select->where(array('id' => $id)); which generates the (My)SQL: WHERE `id` = '2' If you want the SQL generated to use the IN operator, then just pass in an array: $idList = array(1, 3, 4); $select->where(array('id' => $idList)); which generates: WHERE `id` IN ('1', '3', '4') 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: 'module_listener_options' => array( 'config_cache_enabled' => true, 'module_map_cache_enabled' => true, 'cache_dir' => 'data/cache/',… 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: return array( 'application' => array( 'setting_1' => 234, ) );… continue reading.