Developing software in the Real World

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.

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.

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.

Changing the format of a ZendForm DateTime element

If you want to change the format of the value of a DateTime element, the easiest way to do this in your Form class is to do this:

The two things to note: You can't set the format within the array – it has to be via a setFormat() call. If you change the format, you must set the min attribute in the same format, as otherwise it will try to set it with… continue reading.

Thoughts on module directory structure

I've been working on a Zend Framework 2 module within a larger project that doesn't have that many PHP class files. Specifically, it has a controller, a mapper, an entity, a service and a form. As a result, the traditional Zend Framework 2 directory structure for the Account module looks like this (with class names in brackets):

That's a lot of directories for not many files! As a result, I decided to flatten it… continue reading.

Using ZendSession

This is a quick note on how to use ZendSession. Although the component name is ZendSession, you actually interact with ZendSessionContainer to store and retrieve session data:

ZendSessionContainer's constructor takes a string argument which is the name for this container ('SomeKeyName' in this case). It's optional and if you don't set it, then it is set to 'Default'. The name allows you to use the same session keys in different containers. To set data… continue reading.

Integrating BjyAuthorize with ZendNavigation

If you are using BjyAuthorize for ACL configuration and want to use ZendNavigation's ZendAcl integration features, then you need to set the Acl and Role information into ZendNavigation. The easiest way to do this is to add the following to ApplicationModule::onBoostrap():

This assumes that you've set up BjyAuthorize with some resources and rules. For example, in my config/autoload/bjyauthorize.global.php, I have a 'bug' resource and have a rule that allows the reporter role access to… continue reading.

Introducing AkrabatSession

One of the requirements for a new app that I'm writing is that it has a specific session name. In Zend Framework 2, this is done by creating a SessionManager with the correct configuration and then setting the default manager on the Session Container:

Obviously, I need to be able to configure the name (and potentially other session configuration options) from my config/autoload/global.php file and this is a generically useful requirement, so I created… continue reading.