Pragmatism in the real world

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.

Adding Diff syntax highlighting to Sublime Text

My chosen colour scheme for Sublime Text doesn't include support for diff/patch files, so I added my own. To the bottom of my .tmTheme file, I added this just above the closing </array>: <dict> <key>name</key> <string>diff.header</string> <key>scope</key> <string>meta.diff, meta.diff.header</string> <key>settings</key> <dict> <key>foreground</key> <string>#009933</string> </dict> </dict> <dict> <key>name</key> <string>diff.deleted</string> <key>scope</key> <string>markup.deleted</string> <key>settings</key> <dict> <key>foreground</key> <string>#DD5555</string> </dict> </dict> <dict> <key>name</key> <string>diff.inserted</string> <key>scope</key> <string>markup.inserted</string> <key>settings</key> <dict> <key>foreground</key> <string>#3333FF</string> </dict> </dict> <dict> <key>name</key> <string>diff.changed</string> <key>scope</key> <string>markup.changed</string> <key>settings</key> <dict> <key>foreground</key>… continue reading.

Simple logging of ZF2 exceptions

I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, the easiest way to do this is to add a listener to the 'dispatch.error' event and log using ZendLog. To… continue reading.

Notes on embedding fonts in rst2pdf

I wanted to use Helvetica Light in a PDF that I'm creating using rst2pdf and this proved a little tricker than I expected. Note that to use fonts with rst2pdf, you need to install fontconfig using brew: brew install fontconfig Now, on to the problem! In this case, I discovered that setting stdFont: Helvetica-Light or stdFont: HelveticaLight in the style file resulted in Verdana being used in the PDF file! Fortunately, the -v switch to… continue reading.

Setting the umask when using Capistrano

This is one of those posts to remind me how I solved a problem last time! I've recently been using Capistrano for deployment and other remote tasks and it's proving quite useful. One problem I ran into was that the umask was being set to 022 when using Capistrano and 002 when I was ssh'd into the server itself. After a bit of research, I discovered that the secret is to put the umask statement… continue reading.